I got this code snippet:
public static void main(String[] args){
Map<String, Set<String>> map = new HashMap<>();
Set<String> set = new HashSet<>();
set.add("user1");
set.add("user2");
map.put("key1", set);
Map<String, Set<String>> map2 = new HashMap<>();
map2.putAll(map);// I expect all elements are copied
map.get("key1").add("user3");// add 1 element in "map"
System.out.println(map2.get("key1").size()); // "map2" was affected
}
In fact the modification of map's set element affected map2, so the program prints "3" instead of "2"
This is weird, I expect that, as long as I used "putAll" method for the new map2 construction, I think both key and value should be deeply cloned?
How to fix my program and make sure map2 is completed divided from map, while copying all elements from map?
Thanks