Java HashMap.containsValue - 有什么意义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2538011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:03:08  来源:igfitidea点击:

HashMap.containsValue - What's the point?

javahashmap

提问by Frederik

I've got a HashMap and I need to fetch an item by its integer value. I notice there's a containsValue() function, but it would appear I still have to iterate through the map to find the correct index anyway.

我有一个 HashMap,我需要通过它的整数值来获取一个项目。我注意到有一个 containsValue() 函数,但看起来我仍然必须遍历地图才能找到正确的索引。

My question is; why use containsValue() if I'm required to traverse it afterwards?

我的问题是;如果我之后需要遍历它,为什么要使用 containsValue() ?

Also, am I missing the point completely? ;-)

另外,我是否完全忽略了这一点?;-)

采纳答案by Arne Burmeister

A map maps a key to a value. If you have a value and you know the map contains this value, why do you need the key anymore?

映射将键映射到值。如果你有一个值并且你知道地图包含这个值,你为什么还需要这个键?

On the other hand, if you really need the key or you have just a property of the value, you can iterate the entrySet(), check the value and return the key if found:

另一方面,如果你真的需要这个键或者你只有一个值的属性,你可以迭代entrySet(),检查值并在找到时返回键:

for (Map.Entry<Index,Value> entry : map.entrySet()) {
  if (entry.getValue().getXy().equals(xy)) {
    return entry.getKey();
  }
}

回答by Riduidel

A map is a key tovalue store. Saying a value is contained is only given as an indication. I think that to have the bijective link allowing you to retrieve key from value, you'll have to rely upon things like BiMapfrom google-collections

地图是关键,以保值。说包含一个值仅作为指示给出。我认为要让双射链接允许您从值中检索键,您必须依赖于google-collections 中的BiMap 之类的东西

回答by sanjuro

you can use containsValue() in cases where you don't need to traverse the whole hashmap, for example if you want to add key-value pair to hashmap, but before that you want to know if that value is in hashmap. In this case for add operation you don't need to traverse whole hashmap.

您可以在不需要遍历整个 hashmap 的情况下使用 containsValue(),例如,如果您想将键值对添加到 hashmap,但在此之前您想知道该值是否在 hashmap 中。在这种情况下,对于添加操作,您不需要遍历整个哈希图。

回答by Jim Kiley

You aren't required to traverse it afterwards. containsValue()is helpful in situations where you don't need to know precisely where the value you, but rather when you only need to know if it's already in the Map. In situations where you need to know precisely where in the Map the value is, don't bother using containsValue()-- jump right to the iterator and find it.

之后您不需要遍历它。 containsValue()在您不需要确切知道您的值在哪里的情况下很有帮助,而是当您只需要知道它是否已经在 Map 中时。在需要精确知道值在 Map 中的位置的情况下,不要费心使用containsValue()-- 直接跳转到迭代器并找到它。

回答by DaveJohnston

A HashMap (or Map in general) uses key/value pairs. When you add something to the map you must specify a key and it is that key that is used again later when retrieving the value. Based on the implementation of the HashMap, given a key, retrieval of a value is done in O(1) time.

HashMap(或一般的 Map)使用键/值对。当您向地图添加某些内容时,您必须指定一个键,并且稍后检索该值时将再次使用该键。基于 HashMap 的实现,给定一个键,一个值的检索在 O(1) 时间内完成。

containsValue is a useful method for checking that a HashMap contains the value you are looking for, but I don't really see why you are using that to retrieve the value you are looking for??

containsValue 是一种用于检查 HashMap 是否包含您要查找的值的有用方法,但我真的不明白您为什么要使用它来检索您要查找的值?

The correft way to use a map would be something like:

使用地图的正确方法是这样的:

HashMap<Integer, Object> myMap = new HashMap<Integer, Object>();
myMap.put(1, object1);
myMap.put(2, object2);
myMap.put(3, object3);

Now you can get your objects by doing:

现在您可以通过执行以下操作来获取对象:

Object myObject = myMap.get(1);

If you did:

如果你这样做:

myMap.containsValue(1);

myMap.containsValue(1);

this would return false, as 1 is the key, not the value. You could do:

这将返回 false,因为 1 是键,而不是值。你可以这样做:

myMap.containsKey(1);

if you just want to know if it exists, but there is no problem in calling:

如果你只是想知道它是否存在,但是调用没有问题:

Object myObject = myMap.get(99);

it would just return null if there was no key, 99.

如果没有键,它只会返回 null,99。

So basically, the point is, you are correct, there is no point in using containsValue when you are trying to retrieve the value. Use get or containsKey if you want to check for existence first.

所以基本上,关键是,你是对的,当你试图检索值时,使用 containsValue 是没有意义的。如果您想先检查是否存在,请使用 get 或 containsKey。

回答by Vivek Vardhan

Let me reframe this question for Frederik:

让我重新为 Frederik 提出这个问题:

Well, containsValue(), does it compare internally (its input parameter) with every "value" in the hashmap? Or does it uses somehow hashcodeing (or other technique) to generate the result? For the former case, we could simply use an iterator to traverse and match the existence of our value with all the hashmap's "value". The significance of the question is in the performance, or speed!

那么,containsValue() 是否在内部(其输入参数)与哈希图中的每个“值”进行比较?或者它是否以某种方式使用哈希编码(或其他技术)来生成结果?对于前一种情况,我们可以简单地使用迭代器来遍历并将我们的值的存在与所有哈希图的“值”进行匹配。问题的意义在于性能,还是速度!

回答by Alan A Donovan

I think Map.containsValue is a mistake in the design of the Map interface.

我认为 Map.containsValue 是 Map 界面设计的一个错误。

One very occasionally encounters Map implementations that provide a faster-than-linear implementation of containsValue. For example, a map might internally represent each distinct value as a small integer and then use bit patterns to represent sets of values. Such a map might be able to detect in constant time that it had never seen a given value before (though it might still take linear time to return an affirmative result).

一个人偶尔会遇到 Map 实现,它提供了一种比线性更快的 containsValue 实现。例如,映射可能在内部将每个不同的值表示为一个小整数,然后使用位模式来表示值集。这样的映射可能能够在恒定时间内检测到它以前从未见过给定值(尽管返回肯定结果可能仍需要线性时间)。

However, an operation that sometimes takes linear time and sometimes takes constant time is not a useful foundation for a generic algorithm. You can't substitute a LinkedList for an ArrayList and expect things to work well, even though they both support random access in their API. A client that needs a constant-time containsValue must maintain a separate HashSet of values to be assured of good performance. Clients happy with linear-time performance can just write the loop themselves.

然而,有时需要线性时间而有时需要常数时间的操作并不是通用算法的有用基础。您不能用 LinkedList 替换 ArrayList 并期望事情运行良好,即使它们都支持其 API 中的随机访问。需要恒定时间 containsValue 的客户端必须维护一个单独的 HashSet 值以确保良好的性能。对线性时间性能满意的客户可以自己编写循环。

Even if the maintainers of the Map interface also regret adding containsValue, it is of course impossible for them to remove it now.

即使 Map 接口的维护者也后悔添加 containsValue,他们现在当然不可能删除它。