@Test
void testReducing() {
List<Map<String, Integer>> nameScoreMapList = Stream.of(
new HashMap<String, Integer>() {{
put(“苏格拉底”, 1);
put(“特拉叙马霍斯”, 3);
}},
Collections.singletonMap(“苏格拉底”, 2),
Collections.singletonMap(“特拉叙马霍斯”, 1),
Collections.singletonMap(“特拉叙马霍斯”, 2)
).collect(java.util.stream.Collectors.toList());
Map<String, List<Integer>> map = nameScoreMapList.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, p -> {
List<Integer> getNameList = new ArrayList<>();
getNameList.add(p.getValue());
return getNameList;
},
(List<Integer> value1, List<Integer> value2) -> {
value1.addAll(value2);
return value1;
}));
Map<String, Integer> mp1 = nameScoreMapList.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, m -> m.getValue(), Integer::sum));
}