简体   繁体   English

为什么Hashmap允许空键?

[英]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? ". 实际上我读了很多关于这个问题的帖子,但没有得到“ 为什么Hashmap允许空键? ”的确切原因/答案。 Please can anyone give me the exact answer with an example? 请问一个人能给我一个例子的确切答案吗?

One interpretation of your question: 您对问题的一种解释:

why hashmap allowed [only] one null key? 为什么hashmap允许[只]一个空键?

Ask yourself: if HashMap allowed more than one null key, how would the map object distinguish between them? 问问自己:如果HashMap允许多个null键,地图对象如何区分它们?

Hint: there is only one null value. 提示:只有一个null值。


Alternative interpretation of your question 您问题的替代解释

why hashmap allowed [a] null key? 为什么hashmap允许[a] null键?

Because it is useful in some circumstances, and because there is no real semantic need to disallow it 1, 2 . 因为它在某些情况下很有用,并且因为没有真正的语义需要不允许它1,2

By contrast, with TreeMap null keys are disallowed because supporting them would be difficult given the implications of orderings involving null . 相比之下,使用TreeMap不允许使用null键,因为考虑到涉及null的排序的含义,支持它们会很困难。

  • Given that the specified semantics for Comparable is to throw NPE. 鉴于Comparable的指定语义是抛出NPE。
  • Comparator is allowed to order null , but it is not required to. 允许 Comparator器订购null ,但不是必需的。 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. 因此,如果TreeMap允许null ,则地图的行为可能会有所不同,具体取决于是使用Comparator还是Comparable 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. 1 - 至少,这是他们在1998年在Java 1.2中指定HashMap时的观点。从那时起,一些设计师可能已经改变了主意,但由于行为明确指定 ,因此在不破坏兼容性的情况下无法更改。 It won't happen ... 它不会发生......

2 - Support for null keys requires some special case code in HashMap which at least adds complexity to the implementation. 2 - 对null键的支持需要HashMap一些特殊情况代码,这至少增加了实现的复杂性。 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. 目前尚不清楚它是否是HashMap的性能开销,因为即使不允许使用null键,仍然需要对null键进行隐式测试。 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. Java工程师必须意识到拥有null键和值有其用途,比如将它们用于默认情况。 So, they provided HashMap class with collection framework in Java 5 with capability of storing null key and values. 因此,他们在Java 5中为HashMap类提供了具有存储空键和值的能力的集合框架。

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. 在HashMap中插入键值对的put方法检查null键并将其存储在内部表数组的第一个位置。 It isn't afraid of the null values and does not throw NullPointerException like Hashtable does. 它不怕空值,也不像Hashtable那样抛出NullPointerException。

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 这个链接可以回答更多hashmap null key的解释

The javadoc for HashMap.put clearly states: HashMap.put的javadoc明确指出:

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). key == null的特定情况以相同的方式运行:对于null键,您不能有两个不同的映射(就像您不能为任何其他键一样)。

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). 这仍然被视为在HashMap早期实现中所犯的错误 ,不幸的是人们在哪里(依赖?)依赖(以及某些特定的顺序,直到java-8)。 The thing is, having this null key you could always pass some metadata along with your actual data "for free". 问题是,拥有这个null键,你总是可以“免费”传递一些元数据和你的实际数据。 Notice that all new collections ConcurrentHashMap , Map.of (in java-9), etc - all prohibit nulls to start with. 请注意,所有新集合ConcurrentHashMapMap.of (在java-9中)等都禁止以空值开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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