HashMap with null key and null value

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(); } } 

Program Output:

 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 e1, e2, and null as keys are related to each other. Are all three assigned to the same hash code? If yes, why?

Since all three seem different, changing one value changes another. Does this mean that only one entry for the key is made in the HashMap or e1, e2, or null beacause, for all processed as the same key.

+10
source share
5 answers

HashMap does not call hashcode when null is passed as a key, and null Key is treated as a special case.

Input method

HashMap places the null key in bucket 0 and displays null as the key to the passed value. HashMap does this by the structure of linked lists. HashMap uses a linked list data structure internally.

The linked list data structure used by HashMap (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 the Entry class, K is null and the value is mapped to the value passed by the put method.

Get method

If in Hashmap get is checked whether the key will be passed as null. The search value for the null key in bucket 0.

Therefore, in one hashmap object hashmap can be only one null key .

+29
source

If you pass null as the card key, it will go to 0 bucket . All null key values ​​will be sent there. This is why it returns the same value because all the keys that you provide are null and are in the same bucket of your HashMap.

+5
source

Enabling the null key, the Hashmap implementation treats it as a special case and does not call the hashCode method, instead, it stores the Entry object in 0 place in the basket.

+2
source

HashMap can only store one value for each key. If you want to save more values, you need to use MultivalueHashMap (the Google Guava and Apache Commons collections contain implementations of such a map).

e1 and e2 are null since you are not assigning any object to them. Therefore, if you use these variables, the key of this map record is also null, which leads to your result. Null does not have a hash code, but it is transported as a key in the HashMap (there are other Map implementations that do not allow Null as a key).

+1
source

When you put NULL in a HashMap, there is a special check if you are trying to put NULL as a key (called putForNullKey () ). This is a special case and does not work the way you try to put some object that is not null, and as you can see, it does not even go on to calculate the hash.

 public V put(K key, V value) { if (table == EMPTY_TABLE) { inflateTable(threshold); } if (key == null) return putForNullKey(value); int hash = hash(key); int i = indexFor(hash, table.length); for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(hash, key, value, i); return null; } private V putForNullKey(V value) { for (Entry<K,V> e = table[0]; e != null; e = e.next) { if (e.key == null) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(0, null, value, 0); return null; } 
+1
source

Source: https://habr.com/ru/post/1202965/


All Articles