contains

inline operator fun <K, V> Map<out K, V>.contains(key: K): Boolean(source)

Checks if the map contains the given key.

This method allows to use the x in map syntax for checking whether an object is contained in the map.

Since Kotlin

1.1

Samples

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

fun main() { 
   //sampleStart 
   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 
   //sampleEnd
}
inline operator fun <K, V> Map<out K, V>.contains(key: K): Boolean(source)

Checks if the map contains the given key.

This method allows to use the x in map syntax for checking whether an object is contained in the map.

Since Kotlin

1.0

Samples

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

fun main() { 
   //sampleStart 
   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 
   //sampleEnd
}

operator fun <T> Array<out T>.contains(element: T): Boolean(source)
operator fun ByteArray.contains(element: Byte): Boolean(source)
operator fun ShortArray.contains(element: Short): Boolean(source)
operator fun IntArray.contains(element: Int): Boolean(source)
operator fun LongArray.contains(element: Long): Boolean(source)
operator fun BooleanArray.contains(element: Boolean): Boolean(source)
operator fun CharArray.contains(element: Char): Boolean(source)

Returns true if element is found in the array.

Since Kotlin

1.0

operator fun <T> Iterable<T>.contains(element: T): Boolean(source)

Returns true if element is found in the collection.

Since Kotlin

1.0

operator fun FloatArray.contains(element: Float): Boolean(source)
operator fun DoubleArray.contains(element: Double): Boolean(source)

Deprecated

The function has unclear behavior when searching for NaN or zero values and will be removed soon. Use 'any { it == element }' instead to continue using this behavior, or '.asList().contains(element: T)' to get the same search behavior as in a list.

Replace with

any { it == element }

Returns true if element is found in the array.

Since Kotlin

1.0