Java HashMap Get Key from Value Example

This example shows how to get the HashMap key from the value. The example also shows how to get all keys using the value in case the value is mapped to multiple keys of the HashMap object.

How to get key using the value of the HashMap?

The HashMap class is a map implementation that maps keys with values. If you want to get HashMap value using a key, you can use the get method and provide the key for which you want to fetch the value. But what if you want to get key from a value?

The HashMap class does not provide any direct method to get a key from value because it is not built for that purpose. However, there are several ways using which you can do that as given below.

1. Using the keySet method and a for loop (1:1 relationship)

This approach gets all the keys using the HashMap keySet method and iterates over them. If the value mapped to any key is matching with the value for which we want to get the key, it returns the key.

Output

2. Using the entrySet method and a for loop (1:1 relationship)

This approach is similar to the above given approach, but instead of the keys, it fetches the HashMap entries using the entrySet method. You can also use an Iterator instead of the for loop.

Output

3. Using a custom HashMap class (1:1 relationship)

The above two approaches use the built-in methods of the HashMap class to get the HashMap key from the value. This approach creates a custom class that extends the HashMap class having a separate map object which maintains the value to key mappings. It also defines a method to get a key from value as given below.

Output

What about HashMap having the same values mapped to multiple keys?

All the above example works for HashMap that has 1:1 relationship between keys and values. However, the HashMap class allows the same value being mapped to multiple keys. If that is the case, you can use the below given method to get a list of all keys mapped to a particular value.

Output

If the value is mapped to multiple keys in the HashMap, the above method returns a list object containing all the keys. If the value is not mapped to any key, it returns an empty list.

The bad:

Approach 1 and 2 iterates over the HashMap object to find a key mapped to the given value. It is a slow operation in terms of performance especially if the map is large.

Approach 3 creates a custom class by extending the built-in HashMap class. But it needs double the memory because it also needs to maintain the reverse mappings of value to key. Also, removing any element from the map does not remove the corresponding mapping from the reverse map.

The bi-directional maps:

If you are already using the Apache Commons library in your project,  you should use the data structure specifically built for this purpose i.e. bi-directional map implementation. As the name suggests, it maintains the mapping in both directions i.e. key to value and value to key.

Output

Limitations:

As with approach 3 of creating a custom class, the DualHashBidiMap class also maintains two HashMap objects to maintain bi-directional mappings.

Plus, the BidiMap does not allow many to one relationship between values and keys. In other words, it does not allow duplicate keys or values.

Note: If you are using the Google Guava library, you can use the BiMap class to achieve the same (also supports generics).

This example is a part of the Java HashMap tutorial with examples.

Please let me know your views in the comments section below

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *