Get key from value in a Java Map
This post will discuss how to get Map’s key from the value in Java, where there is a 1:1 relationship between keys and values in the map, i.e., no two keys have the same value.
1. Using entrySet() method
The idea is to iterate over all mappings present in the map using the entrySet() method and compare each value with the desired value until we get the corresponding key.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.HashMap; import java.util.Map; class Main { public static <K, V> K getKey(Map<K, V> map, V value) { for (Map.Entry<K, V> entry: map.entrySet()) { if (value.equals(entry.getValue())) { return entry.getKey(); } } return null; } public static void main(String[] args) { Map<String, Integer> hashMap = new HashMap(); hashMap.put("A", 1); hashMap.put("B", 2); hashMap.put("C", 3); System.out.println(getKey(hashMap, 2)); // prints `B` } } |
From Java 8, we can use Stream:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.HashMap; import java.util.Map; class Main { public static <K, V> K getKey(Map<K, V> map, V value) { return map.entrySet().stream() .filter(entry -> value.equals(entry.getValue())) .findFirst().map(Map.Entry::getKey) .orElse(null); } public static void main(String[] args) { Map<String, Integer> hashMap = new HashMap(); hashMap.put("A", 1); hashMap.put("B", 2); hashMap.put("C", 3); System.out.println(getKey(hashMap, 2)); // prints `B` } } |
2. Using keySet() method
We can also iterate over all keys present in the map using the keySet() method and compare each key’s value with the desired value until we get the corresponding key.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.HashMap; import java.util.Map; class Main { public static <K, V> K getKey(Map<K, V> map, V value) { for (K key: map.keySet()) { if (value.equals(map.get(key))) { return key; } } return null; } public static void main(String[] args) { Map<String, Integer> hashMap = new HashMap(); hashMap.put("A", 1); hashMap.put("B", 2); hashMap.put("C", 3); System.out.println(getKey(hashMap, 2)); // prints `B` } } |
From Java 8, we can use Stream:
|
1 2 3 4 5 6 7 |
public static <K, V> K getKey(Map<K, V> map, V value) { return map.keySet() .stream() .filter(key -> value.equals(map.get(key))) .findFirst().get(); } |
3. Using Reverse Map
The idea is to extend the HashMap class and overload its put() method such that it also inserts the value-key pair into a reverse map along with the key-value pair in the original map. We also create a getKey() method that facilitates the value lookup in the reverse map.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.HashMap; import java.util.Map; class MyHashMap<K, V> extends HashMap<K, V> { Map<V, K> reverseMap = new HashMap<>(); @Override public V put(K key, V value) { reverseMap.put(value, key); return super.put(key, value); } public K getKey(V value) { return reverseMap.get(value); } } class Main { public static void main(String[] args) { MyHashMap<String, Integer> hashMap = new MyHashMap(); hashMap.put("A", 1); hashMap.put("B", 2); hashMap.put("C", 3); System.out.println(hashMap.getKey(2)); // prints `B` } } |
4. Using Guava’s BiMap Class
Guava provides a BiMap class, a bidirectional map to provide an inverse view of mappings, i.e., with reversed keys and values. BiMap doesn’t allow duplicate values and throws IllegalArgumentException when multiple entries with the same value are encountered. To get the inverse view of the BiMap, we can use the inverse() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.BiMap; import com.google.common.collect.ImmutableBiMap; class Main { public static <K, V> K getKey(BiMap<K, V> map, V value) { return map.inverse().get(value); } public static void main(String[] args) { BiMap<String, Integer> bimap = ImmutableBiMap.of("A", 1, "B", 2, "C", 3); System.out.println(getKey(bimap, 2)); // prints `B` } } |
5. Using Apache Commons Collections
Like Guava, Apache Commons also facilitates bidirectional lookup between key and values by providing a BidiMap interface. It has several implementations that allow a key to be looked up from a value using the inverseBidiMap() method.
Please note that when multiple entries with the same value are found, the last inserted key is returned.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.apache.commons.collections4.BidiMap; import org.apache.commons.collections4.bidimap.DualHashBidiMap; class Main { public static <K, V> K getKey(BidiMap<K, V> map, V value) { return map.inverseBidiMap().get(value); } public static void main(String[] args) { BidiMap<String, Integer> bimap = new DualHashBidiMap<>(); bimap.put("A", 1); bimap.put("B", 2); bimap.put("C", 3); System.out.println(getKey(bimap, 2)); // prints `B` } } |
That’s all about getting the map key from the value in Java.
Follow-up:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)