hashmap中赋值问题,存取问题

参考:http://hi.baidu.com/sunflower87/item/bf613b045a1f3717addc70ca

 

1, 有一个hashmap1, 将它赋给 hashmap2,当将 hashmap2作改动时,hashmap1也会有变动,如code1。怎么才能让它们俩的操作不相关呢?

HashMap<Integer, String> hashmap1 = new HashMap<Integer, String>();
   hashmap1.put(1, "sunday");
   hashmap1.put(2, "monday");
   hashmap1.put(3, "tuesday");
   hashmap1.put(4, "Wednesday");
   hashmap1.put(5, "Thursday");
   hashmap1.put(6, "Friday");
   hashmap1.put(7, "Saturday");

   System.out.println("the value of map is :" + hashmap1.values());

   Map<Integer, String> testMap = new HashMap<Integer, String>();
   testMap = hashmap1; // 赋值操作
   System.out.println(" 新的 testMap 中值为:-------------");
   System.out.println("testmap:" + testMap.values()+ "\n");
  
   if (Calendar.getInstance().get(Calendar.DAY_OF_WEEK) == 5) {
    testMap.remove(Calendar.getInstance().get(Calendar.DAY_OF_WEEK));
   }
   System.out.println(" 新的 testMap 中,删除一个元素后,为:-------------");
   System.out.println("testMap remove" + testMap.values() + ",week:"
     + Calendar.getInstance().get(Calendar.DAY_OF_WEEK) + "\n");
   System.out.println(" 新的 testMap 中,删除一个元素后,原来 的 hashmap1 为:-------------");
   System.out.println("the result map :" + hashmap1.values());

结果:

the value of map is :[sunday, monday, tuesday, Wednesday, Thursday, Friday, Saturday]
新的 testMap 中值为:-------------
testmap:[sunday, monday, tuesday, Wednesday, Thursday, Friday, Saturday]

新的 testMap 中,删除一个元素后,为:-------------
testMap remove[sunday, monday, tuesday, Wednesday, Friday, Saturday],week:5

新的 testMap 中,删除一个元素后,原来 的 hashmap1 为:-------------
the result map :[sunday, monday, tuesday, Wednesday, Friday, Saturday]

remove后,hashmap1 和 testMap 都没有 Thursday 了。

原因:testmap = hashmap1; 表示testMap和map都指向同一个对象,所以调用testMap.remove也就是调用map.remove,其实都是对同一个对象进行操作。Java值传递,你new的第二个Map没有存放东西,指向了第一个Map对象,两个引用都指向了第一个Map对象,清空操作当然会清空第一个Map的

这里有三个解决方法:

1。建议clone()方法创建新的testMap

2. 将第一个map的值通过遍历的方式赋值给第二个map,这样你操作任意一个map, 
另一个map都不会改变。 见code3.
3. 用putAll方法赋值,本质也是 2 中的方法。见 code2.

code2:

firstMap.put("1", "one");
         firstMap.put("2", "two");
         firstMap.put("3", "three");

         Map<String, String> anotherMap = new HashMap<String, String>();
         anotherMap.putAll(firstMap); //赋值操作
         
         System.out.println("firstMap 赋给 anotherMap 后,firstMap为:" + firstMap.values());
         anotherMap.remove("2");

         System.out.println("anotherMap remove KEY 为 \"2\"后,anotherMap为 :" + anotherMap.values());
         System.out.println("anotherMap remove KEY 为 \"2\"后,firstMap为:" + firstMap.values());

结果:

firstMap 赋给 anotherMap 后,firstMap为:[three, two, one]
anotherMap remove KEY 为 "2"后,anotherMap为 :[three, one]
anotherMap remove KEY 为 "2"后,firstMap为:[three, two, one]

code3:

HashMap<Integer, String> hashmap1 = new HashMap<Integer, String>();
   hashmap1.put(1, "sunday");
   hashmap1.put(2, "monday");
   hashmap1.put(3, "tuesday");
   hashmap1.put(4, "Wednesday");
   hashmap1.put(5, "Thursday");
   hashmap1.put(6, "Friday");
   hashmap1.put(7, "Saturday");

   System.out.println("the value of map is :" + hashmap1.values());

   Map<Integer, String> testMap = new HashMap<Integer, String>();

   Iterator it = (Iterator) hashmap1.entrySet().iterator(); // 赋值操作
   while (((java.util.Iterator<Entry<Integer, String>>) it).hasNext()) {
    Map.Entry entry = (Map.Entry) it.next();
    Integer key = (Integer) entry.getKey();
    Object val = entry.getValue();
    testMap.put(key, (String) val);
   }
  
  
  
   System.out.println(" 新的 testMap 中值为:-------------");
   System.out.println("testmap:" + testMap.values()+ "\n");
  
   if (Calendar.getInstance().get(Calendar.DAY_OF_WEEK) == 5) {
    testMap.remove(Calendar.getInstance().get(Calendar.DAY_OF_WEEK));
   }
   System.out.println(" 新的 testMap 中,删除一个元素后,为:-------------");
   System.out.println("testMap remove" + testMap.values() + ",week:"
     + Calendar.getInstance().get(Calendar.DAY_OF_WEEK) + "\n");
   System.out.println(" 新的 testMap 中,删除一个元素后,原来 的 hashmap1 为:-------------");
   System.out.println("the result map :" + hashmap1.values());

结果:

the value of map is :[sunday, monday, tuesday, Wednesday, Thursday, Friday, Saturday]
新的 testMap 中值为:-------------
testmap:[sunday, monday, tuesday, Wednesday, Thursday, Friday, Saturday]

新的 testMap 中,删除一个元素后,为:-------------
testMap remove[sunday, monday, tuesday, Wednesday, Friday, Saturday],week:5

新的 testMap 中,删除一个元素后,原来 的 hashmap1 为:-------------
the result map :[sunday, monday, tuesday, Wednesday, Thursday, Friday, Saturday]

你可能感兴趣的:(hashmap赋值,hashmap存取)