Java8 Stream应用:Map合并、过滤、遍历、values int求和等
生活随笔
收集整理的這篇文章主要介紹了
Java8 Stream应用:Map合并、过滤、遍历、values int求和等
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. Java多個Map合并
// 多個Map<Long,Integer>, 根據key相同的,value累積求和;
public static Map mapCombine(List<Map<Long,Integer>> list) {Map<Long, Integer> map = new HashMap<>();for (Map<Long,Integer> m : list) {Iterator<Long> it = m.keySet().iterator();while (it.hasNext()) {Long key = it.next();if (!map.containsKey(key)) {map.put(key, m.get(key));} else {map.put(key,(map.get(key)+m.get(key)));}}}return map;
}
2. Java中 map.values(Integer) 求和:
Integer totalCount = map.values().stream().mapToInt(Integer::intValue).sum(); //values求和
3. Map遍歷:
map.entrySet().stream().forEach(x -> {System.out.println("key: "+x.getKey()+", value: "+x.getValue());
});
4. Map 過濾:
result = map.entrySet().stream().filter(map -> "hello world".equals(map.getValue())).map(map -> map.getValue()).collect(Collectors.joining()
);
參考:
https://blog.csdn.net/qq_24877569/article/details/52187388
總結
以上是生活随笔為你收集整理的Java8 Stream应用:Map合并、过滤、遍历、values int求和等的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 气泵多少钱啊?
- 下一篇: 使用Python,OpenCV创建动画G