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 m
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
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.
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!
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.