【问题标题】:Why does this Map gets updated? (JAVA)为什么这张地图会更新? (JAVA)
【发布时间】:2014-05-28 10:46:29
【问题描述】:

我一直在查看 Java 的 Map api,可能是因为当我更新另一个地图 (map2) 时,我的代码中的某个地图 (map1) 也会更新,或者我编写它的方式可能有问题。

void process(Object superObject) {

      Map<Date, Object> map1 = superObject.getValuesForMap();
      Map<Date, Object> map2 = map1;

      updateValueOf(superObject,map2);
}

这就是我更新 map2 值的方式。

updateValueOfMap(Object superObject,Map<Date, Object> map2){

      List<Object> objects = getTheObjectsFromASource;
      for (Object obj : objects) {
         List<Triple<Date, Double, Object>> triples = superObject.getSomeEntriesWithThisAttribute(obj.getCertainAttrib());
         for (Triple<D,D,O> t : triples) {
            Object cache = map2.get(t.first)
            if (cache == null) {
               cache = new Object();
               cache.setThis(t.second);
               cache.setThat(t.third);
            } else {
               Double value = cache.getThis() + t.second; // add the double value from triple to the current cache Object's value
               cache.setThis(value);                      // and update the Object's value in the map
            }
            map2.put(t.first, cache);
         }
  }

}

问题是 superObject.getValuesForMap() 中的某些条目也会更新为与 map2 中的相应条目相同的值,每次迭代 for (Triple..)。为什么呢? 响应将不胜感激。提前致谢!

【问题讨论】:

    标签: map hashmap


    【解决方案1】:

    地图 map1 = superObject.getValuesForMap(); 地图 map2 = map1;

    以上三个,都指向同一个内存位置,所以确实会更新。

    试试这个方法:

    Map map2 = new HashMap();

    map2.putAll(map1);

    更新:下面的示例程序(map1 未随 MAP2 更改而更新。)

    公共类 BaseClass {

    Map<String,String> xx = new HashMap<String,String>();
    
    public BaseClass(){
        xx.put("1", "One");
        xx.put("2", "Two");
        xx.put("3", "Three");
    }
    
    public Map<String,String> getValuesForMap(){
        return xx;
    }
    

    }

    公共类 TestProgram 扩展 BaseClass{

    void process() {
    
          Map<String, String> map1 = getValuesForMap();
          Map<String, String> map2 = new HashMap<String,String>();
              map2.putAll(map1);
          updateValueOf(map1, map2);
    }
    
    public void updateValueOf(Map<String, String> map1, Map<String, String> map2){
        String str1 = map2.get("1");
        str1 = str1+"Item";
        map2.put("1", str1);
    
        String str2 = map2.get("2");
        str2 = str2+"Item";
        map2.put("2", str2);
    
        String str3 = map2.get("3");
        str3 = str3+"Item";
        map2.put("3", str3);
    
        System.out.println("Printing Map1 ");
        printit(map1);
        System.out.println("Printing Map2 ");
        printit(map2);
        System.out.println("Printing Map1 Again");
        printit(map1);
        System.out.println("Printing Map2 Again");
        printit(map2);
    }
    
    
    public void printit(Map<String,String> map){
        Iterator iter = map.entrySet().iterator();
            while (iter.hasNext()) {
            Map.Entry pairs = (Map.Entry)iter.next();
            System.out.println(pairs.getKey() + " = " + pairs.getValue());
        }
    }
    
    public static void main(String[] args){
        TestProgram ts = new TestProgram();
        ts.process();
    }
    

    }

    【讨论】:

    • 两个地图中仍将引用这些值,并将以 OP 似乎没有预料到的方式进行更新。
    • 感谢您指出这一点(内存位置)。当我将 superObject 的值分配给 map2 时,我没有意识到这一点。我尝试像您描述的那样分配它,Puneetsri,但是 map1仍在更新..我将尝试手动分配 superObject 的值。
    • 嗨 el_ementary,刚刚尝试了一个示例程序(请参阅上面的更新),似乎工作正常。Map2 更改不会影响 Map1 项目。我可以尝试使用确切的程序,因为你正忙于,如果需要。如果您在上面的代码中发现任何错误/误解,请指出。
    猜你喜欢
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    相关资源
    最近更新 更多