Best Java Datastructure to store key Value Pair

android, java

Solution

You can use Map with entrySet and Map.Entry class to iterate and get both keys and values even if you don't know any of the keys in the Map.

Map <Integer,String> myMap = new HashMap<Integer,String>();

Iterator<Entry<Integer, String>> iterator = myMap.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<Integer,String> pairs = (Map.Entry<Integer,String>)iterator.next();
    String value =  pairs.getValue();
    Integer key = pairs.getKey();
    System.out.println(key +"--->"+value);
}

Problem

Possible Duplicate: Java Hashmap: How to get key from value? Bi-directional Map in Java? I want a `key value data structure` to use for Android App. I can use `Map<K,V>`, but in `Map` I can't get key for particular Value. Is there any good Java data structure using which I can retrieve key by value and vice-versa.

Original source

Related problems