This article explores different ways to check if a value exists in a Map in Kotlin.

1. Using containsValue() function

The recommended solution to find a value in a Map is using the containsValue() function. It returns true if the map contains one or more keys corresponding to the specified value. If the value is a custom object, don’t forget to override the equals and hashCode method.

Download Code

2. Using any() function

Another plausible way involves getting the mutable collection of the values contained in the map and searching the specified value in it. Here’s a working example:

Download Code

3. Using Loop

Finally, we can write custom logic to determine if a value exists in a Map. Here’s how the code would look like:

Download Code

That’s all about checking if a value exists in a Map in Kotlin.