我不会为此使用任何lambda,但是我已经使用
Map.merge了Java 8中引入的和方法参考。
Map<String, Long> result = new HashMap<>();for (Map<String, Long> map : genderToCountList) for (Map.Entry<String, Long> entry : map.entrySet()) result.merge(entry.getKey(), entry.getValue(), Long::sum);
您也可以使用
Streams 执行此操作:
return genderToCountList.stream().flatMap(m -> m.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, Long::sum));



