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.
Asked
Active
Viewed 807 times
-4
-
Looks like trying to solve issue sitting on interview – Andremoniy Nov 09 '15 at 09:07
1 Answers
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.

Ankur Singhal
- 26,012
- 16
- 82
- 116
-
*If you don't aware of bucket,then keep away man....* - is the response you get when you try to tell the OP the same thing. LMAO – TheLostMind Nov 09 '15 at 09:39
-
@VinodMadyalkar Yes i have gone thru comments, i have flagged it – Ankur Singhal Nov 09 '15 at 09:40
-
He is like lkg student.Don't know how to talk.sitting here to demotivate others. – Anandhu Nov 09 '15 at 10:53