map.put 和 map.putAll 的关系
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("a","1");
map.put("b","2");
System.out.println(map);
HashMap<String, String> map2 = new HashMap<>();
map.put("a","3");
map.put("b","4");
map.put("c","5" );
map.putAll(map2);
System.out.println(map);
}

put 添加进去相同的key 会覆盖之前的value
putAll 有相同的key 会覆盖之前value 没有就会合并
本文详细解析了Java中HashMap的put和putAll方法的使用及区别。通过实例展示了当键相同时,put和putAll如何处理原有值,以及它们在合并多个Map时的行为差异。

455

被折叠的 条评论
为什么被折叠?



