Question

I have my code,

HashMap<String, ArrayList<String>> map1 = new HashMap<String, ArrayList<String>>();
    HashMap<String, ArrayList<String>> map2 = new HashMap<String, ArrayList<String>>();

    ArrayList<String> t1= new ArrayList<String>();
    t1.add("a");t1.add("a");
    t1.add("a");t1.add("a");t1.add("a");

    map1.put("1", t1);
    System.out.print("Map1: "+map1.toString());

    map2.putAll(map1);
    System.out.print("Map2: "+map1.toString());

    map2.get("1").set(0, "b");
    System.out.print("Map1: "+map1.toString()+"  |  "+"Map2: "+map2.toString());

when altering the values in map2, it changes the values in map1. Please help me with this issue as soon as possible.

Thanks

EDIT:We have 3 Answers here. All of them are near to the correct answer. I have selected the best answer.

Was it helpful?

Solution 2

change:

map2.putAll(map1);

to:

map2.put("1",new ArrayList<String>(l1));

EDIT

for(Map.Entry<String, ArrayList<String>> entry: map1.entrySet()){
    map2.put(entity.getKey(),new ArrayList<String>(entity.getValue()));
}

The problem was you were using the same copy of the arraylist in both the map so it ddoesn't matter through which map you change the value, it will change for both.

To make 2 separate copies of the arraylist in each item of the map you need to create a new list with same data. Like the one in the above for loop.

OTHER TIPS

Both map1 and map2 contain the same list. They both contain the reference to the same list. So, if the list is modified the change will be reflected in both the maps.

map1 -------------->t1<----------------map2

If you want them to have separate copies of the list create a new list.

 map1 -------------->t1
 map2 -------------->t1` //where t1` is a new copy of t1

You are the victim of the difference between shallow copy and deep copy.

Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.

Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.

The line "map2.putAll(map1);" essentially creates a shallow copy and both map1 and map2 share the reference to the same data. If you want them to be completely different, it is recommended that you duplicate everything. One possible way to do that is to use the MapEntry and copy each record manually.

    HashMap<String, ArrayList<String>> map1 = new HashMap<String, ArrayList<String>>();
    HashMap<String, ArrayList<String>> map2 = new HashMap<String, ArrayList<String>>();
    ArrayList<String> t1 = new ArrayList<String>();
    t1.add("a");
    t1.add("a");
    t1.add("a");
    t1.add("a");
    t1.add("a");

    map1.put("1", t1);

    for (Map.Entry<String, ArrayList<String>> entry : map1.entrySet()) {
        map2.put(entry.getKey(), new ArrayList<String>());
        for(String s: entry.getValue()) {
            map2.get(entry.getKey()).add(s);
        }
    }
    System.out.println("Map1: " + map1.toString());
    System.out.println("Map2: " + map1.toString());
    map2.get("1").set(0, "b");
    System.out.println("Map1: " + map1.toString() + "  |  " + "Map2: " + map2.toString());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top