类:HashMap
方法:putAll()
作用:将map2的数据全部放入到map1中,如果map1中已经存在key则更新,如果不存在则插入。
测试代码:
public static void putAllTest() {System.out.println("putAll 将map2的数据全部放入到map1中,如果map1中已经存在key则更新,如果不存在则插入。");HashMap<String, String> map1 = new HashMap();HashMap<String, String> map2 = new HashMap();for (int i = 1; i < 5; i++) {map1.put("key" + i, String.valueOf(i));map2.put("key" + i, String.valueOf(i * 10));}System.out.println("before: =======================");map1.keySet().stream().forEach(key -> System.out.println(key + " : " + map1.get(key)));map2.keySet().stream().forEach(key -> System.out.println(key + " : " + map2.get(key)));map1.putAll(map2);System.out.println("after: =======================");map1.keySet().stream().forEach(key -> System.out.println(key + " : " + map1.get(key)));map2.keySet().stream().forEach(key -> System.out.println(key + " : " + map2.get(key)));}
运行结果:
putAll 将map2的数据全部放入到map1中,如果map1中已经存在key则更新,如果不存在则插入。
before: =======================
key1 : 1
key2 : 2
key3 : 3
key4 : 4
key1 : 10
key2 : 20
key3 : 30
key4 : 40
after: =======================
key1 : 10
key2 : 20
key3 : 30
key4 : 40
key1 : 10
key2 : 20
key3 : 30
key4 : 40Process finished with exit code 0