List<Map<Integer,Integer>>中的Map内容合并到一个Map,重复key对应的value相加

上代码:

1 private static  Map<Integer,Integer> itemMap(List<Map<Integer,Integer>> listMap){
2         Map<Integer,Integer> mapAll = new HashMap<>();
3         for(Map<Integer,Integer> map : listMap){
4             for(int i : map.keySet()){
5                 mapAll.put(i,mapAll.containsKey(i) ? mapAll.get(i) + map.get(i) : map.get(i));
6             }
7         }
8         return mapAll;
9     }

对于不同类型的Map有不同的合并方式,更改对应的计算方法即可。

posted @ 2020-11-25 16:46  程序员也需要终身学习  阅读(746)  评论(0)    收藏  举报