Map.putAll()的用法

来源:互联网 发布:淘宝怎么成为大v 编辑:程序博客网 时间:2024/06/14 08:59
 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