I am having trouble with implementing HashMaps
with ArrayLists
in my java class. The thing is it keeps adding objects to the ArrayList
is the HashMap
even though I am not updating my HashMap
.
This is the code that I can not understand how works:
HashMap<String, ArrayList<String>> map = new HashMap<>();
ArrayList<String> array = new ArrayList<String>();
array.add("One");
array.add("Two");
map.put("Key", array);
array.add("Three"); //2. Why does this get added to the HashMap?
System.out.println(map1.get("Key"));
//1. This print out [One, Two, Three].. When it should be [One, Two]!