HashMap.putAll

来源:互联网 发布:在淘宝开店充值赚钱吗 编辑:程序博客网 时间:2024/06/05 20:52



public class HashMapDemo {/** * @param args */public static void main(String[] args) {       HashMap hashMap1 = new HashMap();       hashMap1.put("A1", "1");       hashMap1.put("A2", "2");              HashMap hashMap2 = new HashMap();       hashMap1.put("B1", "1");       hashMap1.put("B2", "2");              HashMap hashMap3 = new HashMap();       hashMap3.putAll(hashMap1);       hashMap3.putAll(hashMap2);       Set entrySet = hashMap3.entrySet();       Iterator iterator = entrySet.iterator();       while(iterator.hasNext()){       Map.Entry next = (Map.Entry)iterator.next();       System.out.println(" key = "+next.getKey()+" value = "+next.getValue());       }       }}

输出结果:

 key = A1 value = 1
 key = B2 value = 2
 key = A2 value = 2
 key = B1 value = 1



修改:

public class HashMapDemo {/** * @param args */public static void main(String[] args) {       HashMap hashMap1 = new HashMap();       hashMap1.put("A1", "1");        hashMap1.put("A2", "2");              HashMap hashMap2 = new HashMap();       hashMap1.put("A1", "1"); //更改       hashMap1.put("A2", "3"); //更改              HashMap hashMap3 = new HashMap();       hashMap3.putAll(hashMap1);       hashMap3.putAll(hashMap2);       Set entrySet = hashMap3.entrySet();       Iterator iterator = entrySet.iterator();       while(iterator.hasNext()){       Map.Entry next = (Map.Entry)iterator.next();       System.out.println(" key = "+next.getKey()+" value = "+next.getValue());       }       }}

输出结果:

 key = A1 value = 1
 key = A2 value = 3





原创粉丝点击