public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
Map<String,Integer> map2 = new HashMap<>();
Map<String,Integer> map3 = new HashMap<>();
map.put("map-1",1);
map.put("map-2",2);
map.put("map-3",3);
map2.put("map-1",2);
map2.put("map-2",1);
map2.put("map-3",0);
map3.put("map-1",1);
map3.put("map-2",1);
map3.put("map-3",1);
Map<String,Integer> mapSum = new HashMap<>();
List<Map<String,Integer>> list = new ArrayList<>();
list.add(map);
list.add(map2);
list.add(map3);
list.stream().forEach(l ->{
Iterator<Map.Entry<String, Integer>> iterator = l.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, Integer> elment = iterator.next();
mapSum.merge(elment.getKey(),elment.getValue(),Integer::sum);
}
});
System.out.println(mapSum.toString());
}
java—map 合并 merge
最新推荐文章于 2024-07-01 00:26:48 发布