¿Por qué se actualiza este mapa? (JAVA)
Frecuentes
Visto 117 equipos
-1
I have been looking through java's api of Map for possible reason why a certain Map (map1) in my code gets updated as well when I update another map (map2) or maybe something is wrong about how I wrote it.
void process(Object superObject) {
Map<Date, Object> map1 = superObject.getValuesForMap();
Map<Date, Object> map2 = map1;
updateValueOf(superObject,map2);
}
This is how I updated the value of 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);
}
}
}
The problem is certain entries in superObject.getValuesForMap() gets updated too with the same value as the corresponding entries in map2 every iteration in the for (Triple..). Why is that so? Responses will be greatly appreciated. Thanks in advance!
1 Respuestas
0
Map map1 = superObject.getValuesForMap(); Map map2 = map1;
All three above , points to the same memory location, so it will indeed be updated.
Prueba de esta manera:
Map map2 = new HashMap();
map2.putAll(map1);
UPDATE : Sample program below(map1 not updated with MAP2 changes.)
public class 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;
}
}
public class TestProgram extends 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();
}
}
Respondido el 02 de junio de 14 a las 17:06
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas dictionary hashmap or haz tu propia pregunta.
The values will still be referenced in both maps, and will be updated in ways the OP appears to not expect. - don roberto
Thank you for pointing that out (memory loc).. I didn't realize that when I assigned superObject's value to map2.. I tried assigning it just like how you described it, Puneetsri, but map1 still gets updated.. I'll try to assign the values of superObject manually. - eLementary
Hi el_ementary, just tried a sample program(see UPDATE above) and seems working fine.Map2 changes didn't affect Map1 items.I can give a try with exact program as you are busy with, if needed.Please point out if you see any error/misunderstanding in the code above. - Puneetsri