Retrieve all keys with specific value in Kotlin Map
This article explores different ways to retrieve all keys with the specified value in Kotlin Map.
1. Create a Reverse Map
The trick is to extend the HashMap class and overload its put() function in such a way that every call to put inserts the key/value pair into the original map and the value-key pair to 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 |
internal class MyHashMap<K, V> : HashMap<K, V?>() { private var reverseMap: MutableMap<V?, MutableSet<K>?> = HashMap() override fun put(key: K, value: V?): V? { if (reverseMap[value] == null) { reverseMap[value] = HashSet() } reverseMap[value]!!.add(key) return super.put(key, value) } fun getKeys(target: V): Set<K>? { return reverseMap[target] } } fun main() { val map: MyHashMap<String?, Int?> = MyHashMap() map["A"] = 1 map["B"] = 1 map["C"] = 2 println(map.getKeys(1)) // [A, B] } |
2. Using entries property
Another solution is to iterate over all entries present in the Map and insert all keys that satisfy the criteria into a collection.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
fun <K, V> getKeys(map: Map<K, V>, target: V): Set<K>? { val keys: MutableSet<K> = HashSet() for ((key, value) in map) { if (target == value) { keys.add(key) } } return keys } fun main() { val map: MutableMap<String?, Int?> = HashMap() map["A"] = 1 map["B"] = 1 map["C"] = 2 println(getKeys(map, 1)) // [A, B] } |
The code can be shortened using the filter() function with the keys property:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
fun <K, V> getKeys(map: Map<K, V>, target: V): Set<K> { return map.filter { (_, value) -> target == value }.keys } fun main() { val map: MutableMap<String?, Int?> = HashMap() map["A"] = 1 map["B"] = 1 map["C"] = 2 println(getKeys(map, 1)) // [A, B] } |
3. Using keys property
Another solution is to iterate over all keys present in the Map using the keys property and compare each key with the desired value. The matching keys are then inserted into a collection.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
fun <K, V> getKeys(map: Map<K, V>, target: V): Set<K>? { val keys: MutableSet<K> = HashSet() for (key in map.keys) { if (target == map[key]) { keys.add(key) } } return keys } fun main() { val map: MutableMap<String?, Int?> = HashMap() map["A"] = 1 map["B"] = 1 map["C"] = 2 println(getKeys(map, 1)) // [A, B] } |
Alternatively, you can call the filter() function on the Set returned by the keys property:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
fun <K, V> getKeys(map: Map<K, V>, target: V): List<K>? { return map.keys.filter { key: K -> target == map[key] } } fun main() { val map: MutableMap<String?, Int?> = HashMap() map["A"] = 1 map["B"] = 1 map["C"] = 2 println(getKeys(map, 1)) // [A, B] } |
That’s all about retrieving all keys with the specified value in Kotlin Map.
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 :)