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
it will return null in first case.
ReplyDelete'D' in second case.
{null=E}
ReplyDeleteBang on the target, the older values get overridden if new values are put into hashtable/hashMap using the same key.
ReplyDeleteI 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