【发布时间】:2019-10-16 10:20:02
【问题描述】:
假设我有一个字符串列表
List<String> fourthList = Arrays.asList("abc","def","ghi");
我想把它转换成像 {1=abc,2=def,3=ghi} 这样的地图。
Java 中的收集器不允许我这样做,因为它只接受 keyMapper 中的方法。
【问题讨论】:
-
这个问题之前没有被问过,或者我可能没有找到它。我能够找到转换列表到映射但是在它提到用户类型变量的所有问题中。我只关心字符串类型的列表。
-
Map
collect = IntStream.rangeClosed(0, FourthList.size() - 1) .boxed() .collect(Collectors.toMap(i -> i + 1, FourthList: :得到)); System.out.println(collect);