-4

In a hashmap what would be the bucket number of null key? I am Trying to learn internals of hashmap if any one can give me good video tutorials it would be appreciated.

Anandhu
  • 57
  • 1
  • 1
  • 10

1 Answers1

1

HashMap, handles null key differently. 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

From HashMap class

 static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

Internally HashMap maintains array of Entry class(internal class of HashMap which is used to store data) which is also called as Bucket. Entry class contains key, value, nextElement, hash-value variables.

Data with null key is stored at Bucket location 0(array index 0 of Entry array). Hash value is also zero for null key.

Source

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116