2

Possible Duplicate:
Java Hashmap: How to get key from value?

I have a hashmap

private static HashMap<ObjectClass, UUID> projectileSet = new HashMap<ObjectClass, UUID>();

and an unfinished method where i want to return the ObjectClass that corresponds to the UUID

public static LegendaryItem getClass(UUID uniqueId) {
    return projectileSet.getKey(uniqueId);

}
Community
  • 1
  • 1
NeonMedusa
  • 95
  • 2
  • 6
  • 14
  • http://www.cs.duke.edu/csl/docs/jgl/api/COM.objectspace.jgl.examples.HashMapExamples.html – Mikhail Jan 16 '13 at 05:23
  • 1
    [Does Java have a HashMap with reverse lookup?](http://stackoverflow.com/q/1670038) – Karthik T Jan 16 '13 at 05:25
  • Also see http://stackoverflow.com/questions/7498751/get-the-keys-with-the-biggest-values-from-a-hashmap (shows iterating EntrySets, which is likely perfectly OK for a reasonable `n` and/or limited number of executions) –  Jan 16 '13 at 05:29

5 Answers5

3

You can return key from HashMap by using "KeySet()" method.

HashMap objH=new HashMap<>();
Set objSet=objH.keySet();
public static LegendaryItem getClass(UUID uniqueId) {
    Iterator objItr=objSet.iterator();
    while(objItr.hasNext()){
        UUID objStr=(UUID) objItr.next();
        if(objStr.equals(uniqueId)){
            return objStr;
        }
    }
}

Iterate the ObjSet and get each key

RevanthKrishnaKumar V.
  • 1,855
  • 1
  • 21
  • 34
Amith
  • 1,907
  • 5
  • 29
  • 48
2

To get such a method as getKey you need a bi directional Hash map which supports lookup from Value to key, as discussed in Does Java have a HashMap with reverse lookup?. Apache Commons(BiDiMap) or Guava(BiMap) seem to offer this feature.

Alternatively you could go the iterative approach that Amith puts forward.

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
1

Dear my friend, there is a problem in your code. When you have unique ids as your values you can use this ids as hashmap key and you dont have to do any complex coding. Just do this:

private static HashMap<UUID, ObjectClass> projectileSet = new HashMap<UUID, ObjectClass>();

and then:

projectileSet.get(uniqueId);
S.Yavari
  • 876
  • 8
  • 25
  • This works to get a Value from a Key, but the question is about how to get a Key for a Value. However, I will humor this suggestion that the original mapping is incorrectly inverted .. +1 –  Jan 16 '13 at 05:34
  • There is a logic that make this incorrect. The values can be same in two different unique keys. On that case your code must return an array of keys that has this value. But if you want a code to do this work tell me and i can write it for you. – S.Yavari Jan 16 '13 at 06:01
  • This is the code you might need: `package test; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; public class CustomHashMap extends HashMap { public ArrayList getKeys(Object value) { ArrayList keys = null; if (containsValue(value)) { keys = new ArrayList(); Set keySet = keySet(); for (Object key : keySet) { if (get(key).equals(value)) keys.add(key); } } return keys; } } ` – S.Yavari Jan 16 '13 at 06:10
0

You cannot get that feature directly in standard java map. Because java map doesn't support inverse view, getting key from given value. For that purpose it is better to use bidirectional map implementation. One of the example : BiMap

RevanthKrishnaKumar V.
  • 1,855
  • 1
  • 21
  • 34
Jon Kartago Lamida
  • 827
  • 1
  • 7
  • 12
0

By using keySet() you can... It will return all keys in the form of set...

Anujith
  • 9,370
  • 6
  • 33
  • 48
Kanagaraj M
  • 956
  • 8
  • 18