简体   繁体   English

Map.containsKey()在没有空值的Map中有用吗?

[英]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 : 尽量不要浪费你的时间考虑性能, containsKeyO(1)时间复杂度

This implementation provides constant-time performance for the basic operations ( get and put ) 此实现为基本操作提供恒定时间性能( getput

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: 不,这会适得其反:

  • In the case when the item is not there, you would see no difference 如果物品不在那里,你会看到没有区别
  • In the case when the item is there, you would end up with two look-ups. 如果物品在那里,你最终会有两个查找。

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.

相关问题 map.containsKey从未在Java中工作 - map.containsKey never worked in java Java map.containsKey不起作用 - Java map.containsKey doesn't work 为什么这个map.put()会覆盖现有的值事件,尽管map.containskey()返回false? - Why this map.put() is overwriting the existing values event though map.containskey() returns false? map.keySet()。contains()和map.containsKey()之间的区别 - Difference between map.keySet().contains() and map.containsKey() 哪个更快? List.contains() 或 Map.containsKey() - Which one is faster? List.contains() or Map.containsKey() 为什么常见的Map实现不会为Map.get()缓存Map.containsKey()的结果 - Why don't common Map implementations cache the result of Map.containsKey() for Map.get() 使用map.get()时使用java Map.containsKey()是多余的 - Is using java Map.containsKey() redundant when using map.get() map.containsKey(key)返回true,但map.get(key)不返回任何内容 - map.containsKey(key) returns true, but map.get(key) doesn't return anything Java Map.containsKey(ArrayList) 在一行中返回 false,在下一行返回 true - Java Map.containsKey(ArrayList) returns false in one line and true in the next 如何基于 containsKey 将值从映射映射到对象? - How to map values from map to object based on containsKey?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM