Consider the following Code :
import java.util.*;
class Employee {
String name;
public Employee(String nm) {
this.name=nm;
}
}
publ
HashMap
does not call hashcode when null is passed as key and null Key is handled as special case.
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 implements Map.Entry {
final K key;
V value;
Entry next;
final int hash;
}
In Entry class the K is set to null and value mapped to value passed in put 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.