猿问

Java 流对地图列表的值求和

我想确定“行”中的“列”或更好的:构建像 List> rows 这样的地图列表的总和


是否有可能以某种方式对每个不同列的所有值求和?该函数应返回一个 Map,以列为键,所有值的总和为值。


summMap.get("columname")

假设我有以下地图列表:


List<Map<String, Long>> mapList = new ArrayList();

Map<String, Object> map1 = new HashMap<>();

Map<String, Object> map2 = new HashMap<>();

Map<String, Object> map3 = new HashMap<>();

map1.put("col1", 90);

map1.put("col2", 50);

map1.put("col3", 10);

map2.put("col1", 90);

map2.put("col2", 50);

map2.put("col3", 10);

map3.put("col1", 90);

map3.put("col2", 50);

map3.put("col3", 10);

mapList.add(map1);

mapList.add(map2);

mapList.add(map3);

Map<String, Long> sum = mapList.stream().distinct().sum() // Example

// result i'm awaiting/expecting

Long sumVal1 = sum.get("col1"); // 270

Long sumVal2 = sum.get("col2"); // 150

Long sumVal3 = sum.get("col3"); // 30

Long sumVal = sum.get("col1");


当年话下
浏览 183回答 3
3回答

桃花长相依

就这么简单Map<String,&nbsp;Long>&nbsp;sum&nbsp;=&nbsp;mapList.stream() &nbsp;&nbsp;&nbsp;&nbsp;.flatMap(m&nbsp;->&nbsp;m.entrySet().stream()) &nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toMap(Map.Entry::getKey,&nbsp;Map.Entry::getValue,&nbsp;Long::sum));

DIEA

霍尔格已经提供了一个干净的解决方案,但我想,你也可以尝试flatMap,并groupingBy为:Map<String, Long> sum = mapList.stream().flatMap(map -> map.entrySet().stream())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(groupingBy(Map.Entry::getKey, summingLong(Map.Entry::getValue)));您问题的完整解决方案:import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import static java.util.stream.Collectors.*;public class ListMapSum {&nbsp; &nbsp; public static void main(String... args) {&nbsp; &nbsp; &nbsp; &nbsp; List<Map<String, Long>> mapList = new ArrayList();&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Long> map1 = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Long> map2 = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Long> map3 = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; map1.put("col1", 90L);&nbsp; &nbsp; &nbsp; &nbsp; map1.put("col2", 50L);&nbsp; &nbsp; &nbsp; &nbsp; map1.put("col3", 10L);&nbsp; &nbsp; &nbsp; &nbsp; map2.put("col1", 90L);&nbsp; &nbsp; &nbsp; &nbsp; map2.put("col2", 50L);&nbsp; &nbsp; &nbsp; &nbsp; map2.put("col3", 10L);&nbsp; &nbsp; &nbsp; &nbsp; map3.put("col1", 90L);&nbsp; &nbsp; &nbsp; &nbsp; map3.put("col2", 50L);&nbsp; &nbsp; &nbsp; &nbsp; map3.put("col3", 10L);&nbsp; &nbsp; &nbsp; &nbsp; mapList.add(map1);&nbsp; &nbsp; &nbsp; &nbsp; mapList.add(map2);&nbsp; &nbsp; &nbsp; &nbsp; mapList.add(map3);&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Long> sum = mapList.stream().flatMap(map -> map.entrySet().stream())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(groupingBy(Map.Entry::getKey, summingLong(Map.Entry::getValue)));&nbsp; &nbsp; &nbsp; &nbsp; Long sumVal1 = sum.get("col1"); // 270&nbsp; &nbsp; &nbsp; &nbsp; Long sumVal2 = sum.get("col2"); // 150&nbsp; &nbsp; &nbsp; &nbsp; Long sumVal3 = sum.get("col3"); // 30&nbsp; &nbsp; }}

噜噜哒

这不支持并行执行,但可以通过修改中的最后一个参数来实现reduce:private static Map<String, Long> reduceLongs(List<Map<String, Long>> maps) {&nbsp; &nbsp; return maps.stream()&nbsp; &nbsp; &nbsp; &nbsp; .flatMap(map -> map.entrySet().stream())&nbsp; &nbsp; &nbsp; &nbsp; .reduce(new HashMap<>(), (map, e) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.compute(e.getKey(), (k ,v) -> v == null ? e.getValue() : e.getValue() + v);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return map;&nbsp; &nbsp; &nbsp; &nbsp; }, (m1, m2) -> { throw new UnsupportedOperationException(); });}并通过测试:final List<Map<String, Long>> maps = new ArrayList<>();Map<String, Long> map1 = new HashMap<>();Map<String, Long> map2 = new HashMap<>();map1.put("col1", 90L);map1.put("col2", 50L);map2.put("col1", 90L);map2.put("col2", 50L);map2.put("col3", 100L);maps.add(map1);maps.add(map2);final Map<String, Long> sums = reduceLongs(maps);assertEquals(180L, sums.get("col1").longValue());assertEquals(100L, sums.get("col2").longValue());assertEquals(100L, sums.get("col3").longValue());
随时随地看视频慕课网APP

相关分类

Java
我要回答