开发者

HashMap.containsValue - What's the point?

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.

My question is; why us开发者_如何学编程e containsValue() if I'm required to traverse it afterwards?

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


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:

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


A map is a key to value 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 BiMap from google-collections


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.


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.

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??

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);

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

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.

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.


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.


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

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).

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.

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


Let me reframe this question for 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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜