Map 为什么要更新此地图?(爪哇)

Map 为什么要更新此地图?(爪哇),map,hashmap,Map,Hashmap,我一直在浏览java的Map api,以了解为什么在我更新另一个Map(map2)时,代码中的某个Map(map1)也会被更新,或者我编写它的方式可能有问题 void process(Object superObject) { Map<Date, Object> map1 = superObject.getValuesForMap(); Map<Date, Object> map2 = map1; updateValueOf(sup

我一直在浏览java的Map api,以了解为什么在我更新另一个Map(map2)时,代码中的某个Map(map1)也会被更新,或者我编写它的方式可能有问题

void process(Object superObject) {

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

      updateValueOf(superObject,map2);
}
void进程(对象超对象){
Map map1=superObject.getValuesForMap();
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);
         }
  }

}
updateValueOfMap(对象超对象,Map map2){
List objects=getTheObjectsFromASource;
用于(对象对象对象:对象){
List triples=superObject.getSomeEntriesWithThisAttribute(obj.getCertainAttrib());
用于(三重t:三重){
对象缓存=map2.get(t.first)
if(缓存==null){
缓存=新对象();
设置这个(t秒);
cache.setThat(t.third);
}否则{
Double value=cache.getThis()+t.second;//将triple中的Double值添加到当前缓存对象的值中
cache.setThis(value);//并在映射中更新对象的值
}
map2.put(t.first,cache);
}
}
}
问题是superObject.getValuesForMap()中的某些条目在for(Triple..)的每次迭代中也会使用与map2中相应条目相同的值进行更新。为什么会这样?
如有答复,将不胜感激。提前谢谢

map1=superObject.getValuesForMap(); map2=map1

以上三个都指向相同的内存位置,因此它确实会被更新

试着这样做:

Map map2=新的HashMap()

map2.putAll(map1)

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

公共类基类{

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;
}
Map xx=newhashmap();
公共基类(){
xx.投入(“1”、“1”);
二十、投入(“2”、“2”);
xx.投入(“3”、“3”);
}
公共地图getValuesForMap(){
返回xx;
}
}

公共类测试程序扩展了基类{

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();
}
void进程(){
Map map1=getValuesForMap();
Map map2=新的HashMap();
map2.putAll(map1);
更新值(map1、map2);
}
公共void updateValueOf(地图map1、地图map2){
字符串str1=map2.get(“1”);
str1=str1+“项目”;
map2.put(“1”,str1);
字符串str2=map2.get(“2”);
str2=str2+“项目”;
map2.put(“2”,str2);
字符串str3=map2.get(“3”);
str3=str3+“项目”;
map2.put(“3”,str3);
System.out.println(“打印地图1”);
打印(map1);
System.out.println(“打印地图2”);
printit(map2);
System.out.println(“再次打印Map1”);
打印(map1);
System.out.println(“再次打印Map2”);
printit(map2);
}
公共作废打印(地图){
迭代器iter=map.entrySet().Iterator();
while(iter.hasNext()){
Map.Entry pairs=(Map.Entry)iter.next();
System.out.println(pairs.getKey()+“=”+pairs.getValue());
}
}
公共静态void main(字符串[]args){
TestProgram ts=新的TestProgram();
ts.过程();
}

}

两个地图中的值仍将被引用,并将以OP似乎不期望的方式进行更新。感谢您指出这一点(内存位置)。。当我将superObject的值赋给map2时,我没有意识到。。我试着像你描述的那样分配它,Puneetsri,但map1仍然会更新。。我将尝试手动分配superObject的值。您好,我只是尝试了一个示例程序(请参阅上面的更新),似乎工作正常。Map2更改不会影响Map1项目。如果需要,我可以在您忙的时候尝试使用确切的程序。如果您在上面的代码中看到任何错误/误解,请指出。