본문 바로가기
코틀린/[Filtering] 필터 작업

[Kotlin][Map] containsKey, containsValue

by jinwo_o 2024. 9. 10.

맵에 지정된 키가 포함되어 있으면 true 를 반환합니다. K 유형의 키를 전달해야 하는 containKey 의 유형 안전성 제한을 극복할 수 있습니다.

val map: Map<String, Int> = mapOf("x" to 1, "y" to 2)
print(map.containsKey("x")) // true
print(map.containsKey("y")) // true
print(map.containsKey("z")) // false

 

맵이 하나 이상의 키를 지정된 값에 매핑하는 경우 true 를 반환합니다. V 유형의 값을 전달해야 하는 containValue 의 유형 안전성 제한을 극복할 수 있습니다.

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

 

containsKey - Kotlin Programming Language

 

kotlinlang.org

 

containsValue - Kotlin Programming Language

 

kotlinlang.org