Consider the following Code :
import java.util.*;
class Employee {
String name;
public Employee(String nm) {
this.name=nm;
}
}
public class HashMapKeyNullValue {
Employee e1;
public void display(){
Employee e2=null;
Map map=new HashMap();
map.put(e2, "25");
System.out.println("Getting the Value When e2 is set as KEY");
System.out.println("e2 : "+map.get(e2));
System.out.println("e1 : "+map.get(e1));
System.out.println("null : "+map.get(null));
map.put(e1, "");
System.out.println("Getting the Value when e1 is set as KEY");
System.out.println("e2 : "+map.get(e2));
System.out.println("e1 : "+map.get(e1));
System.out.println("null : "+map.get(null));
map.put(null, null); // null as key and null as value
System.out.println("Getting the Value when setting null as KEY and null as value");
System.out.println("e2 : "+map.get(e2));
System.out.println("e1 : "+map.get(e1));
System.out.println("null : "+map.get(null));
map.put(null, "30");
System.out.println("Getting the Value when setting only null as KEY");
System.out.println("e2 : "+map.get(e2));
System.out.println("e1 : "+map.get(e1));
System.out.println("null : "+map.get(null));
}
public static void main(String[] args) {
new HashMapKeyNullValue().display();
}
}
The Output of program is :
Getting the Value When e2 is set as KEY
e2 : 25
e1 : 25
null : 25
Getting the Value when e1 is set as KEY
e2 :
e1 :
null :
Getting the Value when setting null as KEY and null as value
e2 : null
e1 : null
null : null
Getting the Value when setting only null as KEY
e2 : 30
e1 : 30
null : 30
Here how e1, e2, and null
as keys are related to each other. Are all three assigned to same hashcode? If yes, WHY?
Since all three look different, the change in one value changes the other. Does it mean, only one entry for key is being made into HashMap
either e1, e2 or null
? Because all are treated to be same key.
HashMap is similar to HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. This class makes no guarantees as to the order of the map.
Yes, HashMap allows null key and null values. 1.2) Can a hashmap have multiple null keys and values? HashMap allows to store one null key and many null values i.e. many keys can have null value in java.
For HashMap, it allows one null key and there is a null check for keys, if the key is null then that element will be stored in a zero location in Entry array. We cannot have more than one Null key in HashMap because Keys are unique therefor only one Null key and many Null values are allowed.
Yes, null is always a valid map key for any type of map key (including primitives, sobjects, and user-defined objects). Do you mean doing something like this? Map<String, String> myMap = new Map<String, String>{}; myMap. put('key1', null);
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<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.
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.
If you pass null
as map key, it will go to 0 bucket
. All values of null key will go there. That is why it returns same value, cause all keys you are providing are null
and are in the same bucket of your HashMap.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With