If you will search around, you'll notice that HashMap
has a special handling for null
keys. null
values are fine as you don't compute hash code for them in a HashMap
. This is the reason why null
keys work fine in HashMap
. As to why Objects.hashCode
works fine, take a look at Rohit's answer.