假设您有一组地图列表,每个地图的键都是在整数范围内的
[0-k]、[0-n]、[0,r]...
,而您的结果地图应该具有
[0-(k+n+r...)]
范围内的键设置,下面的内容应该可以工作:
public static void main(String[] args) throws IOException {
List<Map<Integer,List<String>>> mapList = List.of(
Map.of( 0,List.of("foo","foo"),
1,List.of("bar","bar"),
2,List.of("baz","baz")),
Map.of( 0,List.of("doo","doo"),
1,List.of("gee","gee"),
2,List.of("woo","woo")),
Map.of( 0,List.of("cab","cab"),
1,List.of("kii","kii"),
2,List.of("taa","taa"))
);
AtomicInteger ai = new AtomicInteger();
Map<Integer,List<String>> result =
mapList.stream()
.flatMap(map -> map.values().stream())
.collect(Collectors.toMap(list -> ai.getAndIncrement(), Function.identity()));
result.forEach((k,v) ->{
System.out.println(k + " : " + v);
});
}