java - HashMap with Null Key and Null Value - Stack Overflow
HashMap
does not call hashcode when null is passed as key and null Key is handled as special case.
Put Method
HashMap
puts null key in bucket 0 and maps null as key to passed value. HashMap does it by linked list data structure. HashMap uses linked list data structure internally.
Linked list data structure used by HashMap
(a static class in HashMap.java
)
static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; Entry<K,V> next; final int hash; }
In Entry class the K is set to null and value mapped to value passed in put method.
Get Method
While in Hashmap
get method the checks if key is passed as null. Search Value for null key in bucket 0.
Hence there can only be one null key in one hashmap
object.
Read full article from java - HashMap with Null Key and Null Value - Stack Overflow
No comments:
Post a Comment