0

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` 
asked Mar 3, 2016 at 4:21
4
  • 4
    In map its not possible to directly fetch the key for a given value. Commented Mar 3, 2016 at 4:22
  • 2
    What do you expect if the values are same for multiple keys?? Commented Mar 3, 2016 at 4:24
  • in my case.. values cannot be same at all.. Commented Mar 3, 2016 at 4:28
  • 2
    Possible duplicate of Java Hashmap: How to get key from value? Commented Mar 3, 2016 at 5:12

5 Answers 5

5

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
 }
 }
answered Mar 3, 2016 at 4:39

Comments

1

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.

answered Mar 3, 2016 at 4:29

Comments

1

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.

answered Mar 3, 2016 at 5:16

Comments

0

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.

answered Mar 3, 2016 at 4:30

Comments

0

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();
answered Mar 3, 2016 at 4:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.