Retrieve all Map keys having given value in Java
This post will discuss how to retrieve all map keys having given value in Java.
1. Create Reverse Map
The idea is to decorate the map with our own implementation by extending the HashMap class and overloading its put() method such that for every call to the put method, it not only inserts the key-value pair in the original map but also inserts the value-key pair into a 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 33 34 35 36 37 38 |
import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; class MyHashMap<K, V> extends HashMap<K, V> { Map<V, Set<K>> reverseMap = new HashMap<>(); @Override public V put(K key, V value) { if (reverseMap.get(value) == null) { reverseMap.put(value, new HashSet<K>()); } reverseMap.get(value).add(key); return super.put(key, value); } public Set<K> getKeys(V value) { return reverseMap.get(value); } } // Program to retrieve all map keys having given value in Java class Main { public static void main(String[] args) { MyHashMap<String, Integer> hashMap = new MyHashMap(); hashMap.put("A", 1); hashMap.put("B", 1); hashMap.put("C", 2); System.out.println(hashMap.getKeys(1)); } } |
2. Using entrySet() method
We can also iterate over all entries present in the map using the entrySet() method and insert all keys that satisfy the criteria into a set.
|
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 |
import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; // Program to retrieve all map keys having given value in Java class Main { public static <K, V> Set<K> getKeys(Map<K, V> map, V value) { Set<K> keys = new HashSet<>(); for (Map.Entry<K, V> entry: map.entrySet()) { if (value.equals(entry.getValue())) { keys.add(entry.getKey()); } } return keys; } public static void main(String[] args) { Map<String, Integer> hashMap = new HashMap(); hashMap.put("A", 1); hashMap.put("B", 1); hashMap.put("C", 2); System.out.println(getKeys(hashMap, 1)); } } |
In Java 8 and above, we can do something like:
|
1 2 3 4 5 6 7 |
public static <K, V> Set<K> getKeys(Map<K, V> map, V value) { return map.entrySet().stream() .filter(entry -> value.equals(entry.getValue())) .map(Map.Entry::getKey) .collect(Collectors.toSet()); } |
3. Using keySet() method
Like the entrySet() 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.
|
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 |
import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; // Program to retrieve all map keys having given value in Java class Main { public static <K, V> Set<K> getKeys(Map<K, V> map, V value) { Set<K> keys = new HashSet<>(); for (K key: map.keySet()) { if (value.equals(map.get(key))) { keys.add(key); } } return keys; } public static void main(String[] args) { Map<String, Integer> hashMap = new HashMap(); hashMap.put("A", 1); hashMap.put("B", 1); hashMap.put("C", 2); System.out.println(getKeys(hashMap, 1)); } } |
In Java 8 and above, we can do something like:
|
1 2 3 4 5 6 |
public static <K, V> Set<K> getKeys(Map<K, V> map, V value) { return map.keySet().stream() .filter(key -> value.equals(map.get(key))) .collect(Collectors.toSet()); } |
That’s all about retrieving all Map keys having a given value in Java.
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 :)