简体   繁体   English

为什么 Map 的实现处理 null 的方式不同?

[英]Why does the implementations of Map handle null differently?

Let's take the containsKey(Object key) as example:我们以containsKey(Object key)为例:

  • TreeMap throws a NullPointerException if the key is null.如果键为空,TreeMap 将抛出 NullPointerException。
  • TreeMap returns false if there is no mapping for the key.如果键没有映射,则 TreeMap 返回 false。
  • HashMap doesn't throw a NullPointerException and never returns null. HashMap 不会抛出 NullPointerException 并且从不返回 null。

Lets have a look into the Javadoc for TreeSet:让我们看看 TreeSet 的 Javadoc:

Throws NullPointerException - if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys 抛出 NullPointerException - 如果指定的键为 null 并且此映射使用自然排序,或者其比较器不允许空键

Meaning: those two Map implementations really have different semantics.含义:这两个 Map 实现确实具有不同的语义。 Therefore it makes sense that their individual implementations of "Map" methods do work differently.因此,他们对“Map”方法的单独实现确实以不同的方式工作是有道理的。

One key property of TreeSet is the fact that it supports/requires ordering . TreeSet 的一个关键属性是它支持/需要排序 That doesn't play nicely with null , thus it is "fair" to throw when null is coming in.这与null 效果不佳,因此当null进入时抛出是“公平的”。

Just read the documentation here :只需阅读此处的文档:

Some map implementations have restrictions on the keys and values they may contain.一些地图实现对它们可能包含的键和值有限制。 For example, some implementations prohibit null keys and values, and some have restrictions on the types of their keys.例如,有些实现禁止空键和值,有些实现对其键的类型有限制。 Attempting to insert an ineligible key or value throws an unchecked exception, typically NullPointerException or ClassCastException.尝试插入不合格的键或值会引发未经检查的异常,通常为 NullPointerException 或 ClassCastException。 Attempting to query the presence of an ineligible key or value may throw an exception, or it may simply return false;尝试查询不合格的键或值的存在可能会引发异常,或者它可能只是返回 false; some implementations will exhibit the former behavior and some will exhibit the latter.一些实现会表现出前一种行为,而另一些会表现出后者。

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

相关问题 为什么JAXB对扩展的处理方式不同? - Why does JAXB handle extensions differently? 为什么我的哈希映射代码打印为空? - Why does my hash map code print null? Gson为什么将LinkedListMultimap地图视图序列化为null? - Why does Gson serialize a LinkedListMultimap map view as null? 为什么 Map.of 不允许空键和值? - Why does Map.of not allow null keys and values? 为什么Orika会将嵌套列表的未使用字段映射为null? - Why does Orika map unused fields of nested List as null? 不同的数据库系统实现(例如MySQL和Microsoft SQL Server)是否会对“ DataSource”和“ DriverManager”产生不同的影响? - Does different database system implementations (e.g. MySQL, and Microsoft SQL Server) affect `DataSource` and `DriverManager` differently? 为什么常见的Map实现不会为Map.get()缓存Map.containsKey()的结果 - Why don't common Map implementations cache the result of Map.containsKey() for Map.get() 为什么相同的代码在java中的工作方式不同? - why does the same code work differently in java? 为什么增量与运算 OR 和 AND 的工作方式不同? [等候接听] - Why does incrementation work differently with operations OR and AND? [on hold] 为什么`--var`和`var-1`的工作方式不同? - Why does `--var` and `var-1` work differently?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM