contains Value
Returns true
if the map maps one or more keys to the specified value.
Allows to overcome type-safety restriction of containsValue
that requires to pass a value of type V
.
Samples
import samples.*
import kotlin.test.*
import java.util.*
fun main() {
//sampleStart
val map: Map<String, Int> = mapOf("x" to 1, "y" to 2)
// member containsValue is used
assertTrue(map.containsValue(1))
// extension containsValue is used when the argument type is a supertype of the map value type
assertTrue(map.containsValue(1 as Number))
assertTrue(map.containsValue(2 as Any))
assertFalse(map.containsValue("string" as Any))
// map.containsValue("string") // cannot call extension when the argument type and the map value type are unrelated at all
//sampleEnd
}