public static void main(String[] args) throws Exception {
List<String> stringList = Arrays.asList("tom", "tohka", "yoxino");
// Collectors.toMap
// 第一个参数,map中key怎么生成,Function.identity()的意思是拿stringList中的元素本身
// 第二个参数,map中value怎么生成,我这边是直接在stringList元素后拼接后缀-value
// 第三个参数,map中key重复时怎么处理,我这边写的表达式意思是:保持原有的不变,即不覆盖
Map<String, String> stringMap = stringList.stream()
.collect(Collectors.toMap(Function.identity(), item -> item + "-value", (k1, k2) -> k1));
System.out.println(stringMap);
}
参考:https://rumenz.com/examples/java8/java8-stream-list-to-map.html
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!