Srividya
Srividya

Reputation: 119

How to retrieve a key based on the value in Map Interface Java?

I am currently working on Data Structures for writing a program on encryption and decryption of names. I have a doubt in Map interface. Actually to get the value associated with a key we have get() method in Map interface. But how to retrieve the key of a particular value without iterating through all the key value pairs in Map interface

Thank you

Upvotes: 2

Views: 1178

Answers (6)

Suresh Atta
Suresh Atta

Reputation: 122006

how to retrieve the key of a particular value without iterating through all the key value pairs in Map interface

Key is Key, not the value. You cannot do it. That's not how Map implemented.

Even If you make it with some magic (iterating multiple times, checking for equls etc ..), that's not guaranteed to give expected result..

And as per the definition of Map, Key is unique not the value. So there will be duplicated values and when you get by value, which associated key you will expect to get ?

If you are sure that there are no duplicates, you can do

 for (Entry<Integer, String> entry : testMap.entrySet()) {
            if (entry.getValue().equals("c")) {
                System.out.println(entry.getKey());
            }
        }

Upvotes: 2

Raphael Roth
Raphael Roth

Reputation: 27373

Given that values are unique, you could to it like this:

    Map<String, String> map = new HashMap<>();
    map.put("key1", "value1");
    map.put("key2", "value2");

    String key = map.entrySet().stream().
    collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))
    .get("value1");

    System.out.println(key); //gives key1

Upvotes: 2

Denis Lukenich
Denis Lukenich

Reputation: 3164

As said, that is not provided by Java-Map interface, since you should have a key and get the value then.

Usually you have something like this.

User user = ...;
HashMap<String, User> usernamesToUser = ...

Then you can get the key by something like:

String username = user.getUsername();

So without using the map actually. However what you can do is, if the key is not directly to retrieve from the object you can use two Maps for both directions. So consider former example (just assume User the User object does not safe the Username)

Map<User, String> userMapReverse = ....;
Map<String, User> userMap = ....;
String username = userMapReverse.get(user);

However this option requires that you maintain two maps, which can be pretty ugly sometimes.

Upvotes: 0

Ilya Novoseltsev
Ilya Novoseltsev

Reputation: 1873

As others have said, it can't be done. The Map interface and its implementations do not support that.

Consider using a BiMap such as the one inculed in Google Guava Collections. It establishes a one-to-one (bidirectional) relationship between keys and values. https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#BiMap

Using BiMap you can use Key key = biMap.inverse().get(value) to get a key for a given value.

Upvotes: 2

Mark Stroeven
Mark Stroeven

Reputation: 696

Well like everyone said, you can't really do it in a decent way because there can be duplicate values. You could search for a hit using the equals() method and compare values. but then again, why are you even using a key/value map if you wanted to do such a thing.

in short, you could write your own version of the get method, taking in a instance of the value object you are trying to get. But there really is not a point in using a map if you are going to do that.

Why not use a list of some sorts of you desire to search on value ?

Upvotes: 0

David Herrero
David Herrero

Reputation: 714

You can not do that because the "values" can be duplicated.

Upvotes: 0

Related Questions