List<String> list = hmap.getOrDefault(key,new ArrayList<String>());
list.add(s);
hmap.put(key,list);
Why do I need to use hmap.put method again?
My understanding is this.
Assume there is no existing key value pair. So, with
List<String> list = hmap.getOrDefault(key,new ArrayList<String>());
I am mapping key with a new instance of the ArrayList.
If I then add a string to the same instance then the hashmap also should get updated automatically right? key => List Object. If I add to the list object then the object reference should not change right?
Why do I need to again use the put method and do this?
hmap.put(key,list);
getOrDefault
is pretty clear about notput
ting anything anywhere.