简体   繁体   中英

Java Map get key from value

below is my code...

Map<Integer, String> MyType = sessionInfo.getType();
//{2=somename} 

I am trying to get key from value...without running any loops....is it possible?

MyType.get("somename") // should output 2` 

It's not easy to get key from value in Hashtable or HashMap, as compared to getting value from key, because Hash Map or Hashtable doesn't enforce one to one mapping between key and value inside Map in Java. infact Map allows same value to be mapped against multiple keys inside HashMap, Hashtable or any other Map implementation.

        String key= null;
        String value="somename";
        for(Map.Entry entry: MyType.entrySet()){
            if(value.equals(entry.getValue())){
                key = entry.getKey();
                break; //breaking because its one to one map
            }
        }

I would encourage running a loop for simplicity. It most likely will not slow down your program a noticeable amount.

However, if you must not run a loop, Google's Guava library has a BiDirectional Map Collection called BiMap that can be ( found here ). The map works both ways and is guaranteed to be synchronized at all times. I also am assuming that you have unique values in your map. If you do not, duplicate values will not have a specific key to link to.

BiMap<String, Integer> biMapInversed = biMap.inverse(); // how to get inverted map

Again, I wouldn't encourage this unless absolutely necessary. Looping through will work perfectly fine in most cases.

Taken from this SO answer

If you choose to use the Commons Collections library instead of the standard Java Collections API, you can achieve this with ease.

The BidiMap interface in the Collections library is a bi-directional map, allowing you to map a key to a value (like normal maps), and also to map a value to a key, thus allowing you to perform lookups in both directions. Obtaining a key for a value is supported by the getKey() method .

There is a caveat though, bidi maps cannot have multiple values mapped to keys, and hence unless your data set has 1:1 mappings between keys and values, you cannot use bidimaps.

This is not possible. You need to consider the value may be duplicated in map. Ex, How do you deal with {2=somename} and {5=somename}

You still need to use a for loop to check value and get key and decide to break or go on when value is matched.

If you're sure that your values are unique you can iterate over the entries of your old map .

Map<String, Character> myNewHashMap = new HashMap<>();
for(Map.Entry<Character, String> entry : myHashMap.entrySet()){
    myNewHashMap.put(entry.getValue(), entry.getKey());
}

Alternatively, you can use a Bi-Directional map like Guava provides and use the inverse() method :

BiMap<Character, String> myBiMap = HashBiMap.create();
myBiMap.put('a', "test one");
myBiMap.put('b', "test two");

BiMap<String, Character> myBiMapInversed = myBiMap.inverse();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM