How java HashMap handles null keys and null values.

Abhimanyu
1 min readNov 17, 2018

When we put a null key to java hashmap hashcode() method is not called on the null, instead puts the key in bucket 0. Java uses linked list to manage multiple objects in the bucket. So if there are already objects in bucket 0, null object will be appended to the linkedlist of bucket 0.

While getting value at null key from HashMap, Java searches for value at null key in bucket 0.

There can be only one null key in Java HashMap.

Since Java 1.8 LinkedList in a bucket can be replaced with a Balanced Tree when the size of the linked list grows beyond a threshold value.

--

--