public void test4(){ Map<String, Object> retMap2 = new HashMap<String, Object>(); retMap2.put("id","2"); retMap2.put("name","xuesiyuan"); retMap2.put("parents","FatherAndMother"); Map<String, Object> retMap = new HashMap<String, Object>(); retMap.put("id","1"); retMap.put("name","xsy"); retMap.put("age","22"); retMap.put("sex","man"); retMap2.putAll(retMap);// System.out.println(retMap2.get("id")+" "+retMap2.get("name")+" "+retMap2.get("age")+" "+retMap2.get("sex")+" "+retMap2.get("parents")); }
Run:1 xsy 22 man FatherAndMother如果new一个Map使用此方法,很容易理解为复制,但是不是,实际上是合并
.putAll(Map)意思是合并两个Map,如果有相同的Key时,后面的Map覆盖前面的Key和Value
Map.putAll()的用法
最新推荐文章于 2022-07-11 11:39:17 发布