How java HashMap handles null keys and null values.
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 aBalanced Tree
when the size of the linked list grows beyond a threshold value.
Read full article from How java HashMap handles null keys and null values.
No comments:
Post a Comment