The syntax of Map.containsValue() function is:
abstract fun containsValue(value: V): Boolean
This containsValue() function of Map returns true if the map maps one or more keys to the specified value.
In this example,
map
with integer keys and character values.containsValue()
function to check if the map contains the value 'b'
.true
or false
, is printed to standard output.fun main(args: Array<String>) {
val map = mapOf(1 to 'a', 2 to 'b', 3 to 'c')
val result = map.containsValue('b')
println(result)
}
true
In this example,
map
with string keys and integer values.containsValue()
function to check if the map contains the value 2
.true
or false
, is printed to standard output.fun main(args: Array<String>) {
val map = mapOf("apple" to 1, "banana" to 2, "cherry" to 3)
val result = map.containsValue(2)
println(result)
}
true
In this example,
map
with integer keys and character values.'c'
using string interpolation.true
or false
, is interpolated into the output string.fun main(args: Array<String>) {
val map = mapOf(1 to 'a', 2 to 'b', 3 to 'c')
println("Contains 'c': ${map.containsValue('c')}")
}
Contains 'c': true
In this Kotlin tutorial, we learned about containsValue() function of Map: the syntax and few working examples with output and detailed explanation for each example.