简体   繁体   中英

Why does Hashmap allow a null key?

Actually I read lots of posts for this question, but did not get the exact reason/answer for " Why does Hashmap allow a null key? ". Please can anyone give me the exact answer with an example?

One interpretation of your question:

why hashmap allowed [only] one null key?

Ask yourself: if HashMap allowed more than one null key, how would the map object distinguish between them?

Hint: there is only one null value.


Alternative interpretation of your question

why hashmap allowed [a] null key?

Because it is useful in some circumstances, and because there is no real semantic need to disallow it 1, 2 .

By contrast, with TreeMap null keys are disallowed because supporting them would be difficult given the implications of orderings involving null .

  • Given that the specified semantics for Comparable is to throw NPE.
  • Comparator is allowed to order null , but it is not required to. And many common implementations don't.

So if null was allowed with TreeMap , then the behavior of a map could be different depending on whether a Comparator or a Comparable is used. Messy.


1 - At least, that was the view when they specified HashMap in Java 1.2 back in 1998. Some of the designers may have changed their minds since then, but since the behavior is clearly specified it cannot be changed without messing up compatibility. It won't happen ...

2 - Support for null keys requires some special case code in HashMap which at least adds complexity to the implementation. It is not clear if it is a performance overhead for HashMap , since there would still need to be an implicit test for a null keys even if null keys were not allowed. This is most likely down in the noise.

Java engineers must have realized that having a null key and values has its uses like using them for default cases. So, they provided HashMap class with collection framework in Java 5 with capability of storing null key and values.

The put method to insert key value pair in HashMap checks for null key and stores it at the first location of the internal table array. It isn't afraid of the null values and does not throw NullPointerException like Hashtable does.

Now, there can be only one null key as keys have to be unique although we can have multiple null values associated with different keys.

this link can answer more hashmap null key explained

The javadoc for HashMap.put clearly states:

Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

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).

This is still viewed as a mistake done in early implementations of HashMap that unfortunately people where (are?) relying on (as well as some particular order until java-8). The thing is, having this null key you could always pass some metadata along with your actual data "for free". Notice that all new collections ConcurrentHashMap , Map.of (in java-9), etc - all prohibit nulls to start with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM