most of us know a hashMap allows null keys and also null values , but what if we add two values corresponding to same null key
map.put(null,null);
map.put(null,"D");
map.put(null,"E");
What will be the output of
System.out.println(map)
will for the null key it will print the last key value pair , i.e.
as per my understanding the null key is unique and on adding more value elements to the same null key will override the old values
3 comments:
it will return null in first case.
'D' in second case.
{null=E}
Bang on the target, the older values get overridden if new values are put into hashtable/hashMap using the same key.
I posted a workaround for this a couple of days back, using an arrayList as a value is a better option, check out this example for adding multiple values to a key in hashmap :)
http://lifeofalazycoder.blogspot.com/2009/05/java-adding-multiple-values-to-key-in.html
Post a Comment