[英]Is Map.containsKey() useful in a Map that has no null values?
In the following piece of code: 在下面的代码中:
if (map.containsKey(key)) {
map.remove(key);
}
Looking at performance, is it useful to first do a Map.containsKey()
check before trying to remove the value from the map? 查看性能,在尝试从地图中删除值之前首先执行Map.containsKey()
检查是否有用?
Same question goes for retrieving values, is it useful to first do the contains check if you know that the map contains no null
values? 同样的问题是检索值,如果您知道地图不包含null
值,首先执行包含检查是否有用?
if (map.containsKey(key)) {
Object value = map.get(key);
}
remove
returns null
if there's no mapping for key
no exception will be thrown: 如果没有key
映射,则remove
返回null
,不会抛出异常:
public V remove(Object key)
I don't see any reason to perform that if
before trying to remove a key
, perhaps maybe if you want to count how many items where removed from the map.. 我看不出有任何理由来执行if
试图删除前key
,也许也许,如果要统计有多少个项目,其中从地图上删除..
In the second example, you'll get null
if the key
doesn't exist. 在第二个示例中,如果key
不存在,则将为null
。 Whether to check or not, depends on your logic. 是否检查,取决于您的逻辑。
Try not to waste your time on thinking about performance, containsKey
has O(1) time complexity : 尽量不要浪费你的时间考虑性能, containsKey
有O(1)时间复杂度 :
This implementation provides constant-time performance for the basic operations (
get
andput
) 此实现为基本操作提供恒定时间性能(get
和put
)
is it useful to first do a Map.containsKey() check before trying to remove the value from the map? 在尝试从地图中删除值之前首先执行Map.containsKey()检查是否有用?
No, it is counterproductive: 不,这会适得其反:
If you want to remove the item unconditionally, simply call map.remove(key)
. 如果要无条件地删除该项,只需调用map.remove(key)
。
Same question goes for retrieving values 同样的问题是检索值
Same logic applies here. 这里适用相同的逻辑。 Of course you need to check the result for null
, so in this case if
stays there. 当然你需要检查结果为null
,所以在这种情况下, if
留在那里。
Note that this cleanup exercise is about readability first, and only then about performance. 请注意,此清理练习首先是关于可读性,然后是关于性能的。 Accessing a map is a fast operation, so accessing it twice is unlikely to cause major performance issues except for some rather extreme cases. 访问地图是一种快速操作,因此访问它两次不太可能导致重大性能问题,除了一些相当极端的情况。 However, removing an extra conditional will make your code more readable, which is very important. 但是,删除额外的条件将使您的代码更具可读性,这非常重要。
The Java documentation on remove()
states that it will remove the element only if the map contains such element. remove()
上的Java文档声明只有在map包含这样的元素时才会删除元素。 So the contains()
check before remove()
is redundant. 所以remove()
之前的contains()
检查是多余的。
This is subjective (and entirely a case of style), but for the case where you're retrieving a value, I prefer the contains(key)
call to the null check. 这是主观的(完全是样式的情况),但是对于你正在检索值的情况,我更喜欢对null检查的contains(key)
调用。 Boolean comparisons just feel better than null comparisons. 布尔比较感觉比空比较好。 I'd probably feel differently if Map<K,V>.get(key)
returned Optional<V>
. 如果Map<K,V>.get(key)
返回Optional<V>
Map<K,V>.get(key)
我可能会有不同的感受。
Also, it's worth noting the "given no null keys" assertion is one that can be fairly hard to prove, depending on the type of the Map (which you might not even know). 此外,值得注意的是“给定无空键”断言是一个相当难以证明的断言,具体取决于Map的类型(您可能不知道)。 In general I think the redundant check on retrieval is (or maybe just feels ) safer, just in case there's a mistake somewhere else (knocks on wood, checks for black cats, and avoids a ladder on the way out). 一般来说,我认为对检索的冗余检查是(或者只是感觉 )更安全,以防万一在其他地方出现错误(敲木头,检查黑猫,并避免出路上的梯子)。
For the removal operation you're spot on. 对于移除操作,您可以找到。 The check is useless. 检查没用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.