본문 바로가기
코틀린/[Elements] 요소 작업

[Kotlin][Collection] contains

by jinwo_o 2024. 8. 19.

컬렉션에서 요소가 발견되면 true 를 반환합니다.

public operator fun <@kotlin.internal.OnlyInputTypes T> Iterable<T>.contains(element: T): Boolean {
    if (this is Collection)
        return contains(element)
    return indexOf(element) >= 0
}


val map: Map<String, Int> = mapOf("x" to 1)

println("map.contains(\"x\") is ${map.contains("x")}") // true
println("\"x\" in map is ${"x" in map}") // true

println("map.contains(\"y\") is ${map.contains("y")}") // false
println("\"y\" in map is ${"y" in map}") // false

 

contains - Kotlin Programming Language

 

kotlinlang.org