Search code examples
androidkotlinhashmap

How to get key for value from Hashmap in Kotlin?


I have HashMap in Kotlin

val map = HashMap<String, String>()

I want to know how to get key for a particular value from this HashMap without iterating through complete HashMap?


Solution

  • Using filterValues {}

    val map = HashMap<String, String>()
    val keys = map.filterValues { it == "your_value" }.keys
    

    And keys will be the set of all keys matching the given value