14

I was just reading about the difference between HashMap and HashTable class in java. There I found a difference that former allow null key and later doesn't privileges for the same. As far as the working of HashMap is concern I know that, it calls hashcode method on key for finding the bucket in which that key value pair is to be placed. Here comes my question: How hashcode for a null value is computed or Is there any default value for hashcode of null key (if so please specify the value)?

6 Answers 6

17

from HashMap:

public V put(K key, V value) {
   if (key == null)
      return putForNullKey(value);
   ...

and if you look further you will see that null always goes to bin 0

4
  • That I got from the HashMap class code but what is the hashcode generated for putting that key value pair in that bin 0 means what is the hashcode value of bin 0.
    – Prashant
    Jun 24, 2013 at 4:59
  • it means the hashcode for null is 0, or (in theory), any numer that upholds num & (tableSize-1) == 0
    – radai
    Jun 24, 2013 at 5:12
  • Do you want to say that for the null key, default value of hashcode is 0 and the first bin/bucket will be used to place it as per the HashMap implementation.
    – Prashant
    Jun 24, 2013 at 5:49
  • jshell> Objects.hash(null). $11 ==> 0 May 16, 2020 at 22:24
6

From the source code of HashMap, if the key is null it is handled differently. There is no hashcode generated for null, but it is uniquely stored at index 0 in an internal array with hash value 0. Also note that hash value of an empty string also is 0(in case keys are strings), but the index where it is stored in the internal array ensures that they are not mixed up.

 /**
 * Offloaded version of put for null keys
 */
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;
}
3

If you read description of static int hash(int h) method in HashMap you will find that null keys have index 0.

0

When a null value is existing in the map the key of that value is also null. you can not have many null keys in a map. Only one null key.

0

It clearly states what happens when you do a put with a key which was already in the map. The specific case of key == null behaves in the same way: you can't have two different mappings for the null key (just like you can't for any other key). It's not a special case, for the context of your question.

0

Internally Hashmap have a nullcheck for key. If it is null then it will return 0 else hash value of key.

where as Hastable doesn't have any null check and it will call directly hashcode method on key

that's why Hashtable won't accepts null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.