合并俩Map相同的key,value相加,不同的key追加
代码如下
Map<String, Double> map1 = new HashMap<String, Double>();
map1.put("one", 1.1);
map1.put("two", 2.1);
map1.put("three", 3.1);
Map<String, Double> map2 = new HashMap<String, Double>();
map2.put("one", 1.1);
map2.put("two", 2.1);
for (String key : map1.keySet()) {
if (map2.containsKey(key)) {
map2.put(key, map1.get(key) + map2.get(key));
} else {
map2.put(key, map1.get(key));
}
}
System.out.println(map2);