Spec-Zone.ru › Kotlin 1.8

containsValue

kotlin-stdlib / kotlin.collections / containsValue
Требования к платформе и версии: JVM (1.0), JS (1.0), Native (1.0)
fun <K, V> Map<K, V>.containsValue(value: V): Boolean

Возвращает true , если карта сопоставляет одну или несколько ключей со значением.

Позволяет обойти ограничения безопасности типов containsValue, которые требуют передать значение типа V.

import kotlin.test.*
import java.util.*

fun main(args: Array<String>) {
//sampleStart
val map: Map<String, Int> = mapOf("x" to 1, "y" to 2)

// member containsValue is used
println("map.containsValue(1) is ${map.containsValue(1)}") // true

// extension containsValue is used when the argument type is a supertype of the map value type
println("map.containsValue(1 as Number) is ${map.containsValue(1 as Number)}") // true
println("map.containsValue(2 as Any) is ${map.containsValue(2 as Any)}") // true

println("map.containsValue(\"string\" as Any) is ${map.containsValue("string" as Any)}") // false

// map.containsValue("string") // cannot call extension when the argument type and the map value type are unrelated at all
//sampleEnd
}

© 2010–2022 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/contains-value.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API