tags:

views:

232

answers:

6

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 use containsValue() if I'm required to traverse it afterwards?

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

+5  A: 

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

Riduidel
A: 

If you can test to see if the map contains a value, then that means you already have it, no?

Jeremy
Yes, i'm sorry about the bad choice of words on my part. What I should have said was that I was wondering why there wasn't something like the IndexOf() function in the String class.I mean, containsValue is there, you would thought it was simple to have it return the actual key instead of true/false, considering the keys can't be null anyway :-)
Frederik
Ah, ok. Map algorithms (I'm thinking a hash table in particular) are generally designed to be going from key to value in such that going backwards would either be impossible or slow. In the case of a HashMap, iterating over the values does not require any knowledge of the keys. At least that's how I understand it.
Jeremy
A: 

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.

sanjuro
containsValue() will traverse the HashMap: http://www.docjar.com/html/api/java/util/HashMap.java.html#631If he wants to know whether a value is in the Map, this information should be kept in a separate Set.
Christoffer Hammarström
yes i know but his question was "why use containsValue() if I'm required to traverse it afterwards?" so i translated it to why is containsValue good for if he needs to traverse the hashmap afterwards(for the second time).
sanjuro
A: 

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.

Jim Kiley
+1  A: 

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.

DaveJohnston
A: 

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();
  }
}
Arne Burmeister