Map.putall()方法

来源:互联网 发布:windows 8 10 whql 编辑:程序博客网 时间:2024/06/01 10:34

将一个已有Map中的数据压入另一个Map中,且去重。

public static void main(String []args){         Map map1 = new HashMap();      //定义Map集合对象            map1.put("apple", "新鲜的苹果");     //向集合中添加对象            map1.put("computer", "配置优良的计算机");            map1.put("book", "堆积成山的图书");            System.out.println("第一个Map集合大小为:"+map1.size()); //输出集合长度            Map map2 = new HashMap();      //定义Map集合map2            map2.put("apple2", "新鲜的苹果");     //向集合中添加对象            map2.put("computer2", "配置优良的计算机");            map2.put("book", "堆积成山的图书");            System.out.println("第二个Map集合大小为:"+map2.size()); //输出集合长度            System.out.println("把第二个Map集合添加到第一个Map集合中");            map1.putAll(map2);        //将map2中的对象添加到map1中            System.out.println("整合后的第一个Map集合大小为:"+map1.size());    }

输出结果为:
代码运行结果
整合后的Map大小不等于6,而是5,HashMap具有去除相同key的二元组。

https://book.2cto.com/201309/31811.html