Thursday, January 22, 2009

HashMap and null keys

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


What will be the output of

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


Anonymous said...

it will return null in first case.
'D' in second case.

Anonymous said...


lazycoder said...

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 :)