W3cubDocs

/Kotlin

any

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T> Array<out T>.any(): Boolean
fun ByteArray.any(): Boolean
fun ShortArray.any(): Boolean
fun IntArray.any(): Boolean
fun LongArray.any(): Boolean
fun FloatArray.any(): Boolean
fun DoubleArray.any(): Boolean
fun BooleanArray.any(): Boolean
fun CharArray.any(): Boolean
@ExperimentalUnsignedTypes fun UIntArray.any(): Boolean
@ExperimentalUnsignedTypes fun ULongArray.any(): Boolean
@ExperimentalUnsignedTypes fun UByteArray.any(): Boolean
@ExperimentalUnsignedTypes fun UShortArray.any(): Boolean

Returns true if array has at least one element.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val emptyList = emptyList<Int>()
println("emptyList.any() is ${emptyList.any()}") // false

val nonEmptyList = listOf(1, 2, 3)
println("nonEmptyList.any() is ${nonEmptyList.any()}") // true
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <T> Array<out T>.any(
    predicate: (T) -> Boolean
): Boolean
inline fun ByteArray.any(
    predicate: (Byte) -> Boolean
): Boolean
inline fun ShortArray.any(
    predicate: (Short) -> Boolean
): Boolean
inline fun IntArray.any(predicate: (Int) -> Boolean): Boolean
inline fun LongArray.any(
    predicate: (Long) -> Boolean
): Boolean
inline fun FloatArray.any(
    predicate: (Float) -> Boolean
): Boolean
inline fun DoubleArray.any(
    predicate: (Double) -> Boolean
): Boolean
inline fun BooleanArray.any(
    predicate: (Boolean) -> Boolean
): Boolean
inline fun CharArray.any(
    predicate: (Char) -> Boolean
): Boolean
inline fun <T> Iterable<T>.any(
    predicate: (T) -> Boolean
): Boolean
@ExperimentalUnsignedTypes inline fun UIntArray.any(
    predicate: (UInt) -> Boolean
): Boolean
@ExperimentalUnsignedTypes inline fun ULongArray.any(
    predicate: (ULong) -> Boolean
): Boolean
@ExperimentalUnsignedTypes inline fun UByteArray.any(
    predicate: (UByte) -> Boolean
): Boolean
@ExperimentalUnsignedTypes inline fun UShortArray.any(
    predicate: (UShort) -> Boolean
): Boolean

Returns true if at least one element matches the given predicate.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val isEven: (Int) -> Boolean = { it % 2 == 0 }
val zeroToTen = 0..10
println("zeroToTen.any { isEven(it) } is ${zeroToTen.any { isEven(it) }}") // true
println("zeroToTen.any(isEven) is ${zeroToTen.any(isEven)}") // true

val odds = zeroToTen.map { it * 2 + 1 }
println("odds.any { isEven(it) } is ${odds.any { isEven(it) }}") // false

val emptyList = emptyList<Int>()
println("emptyList.any { true } is ${emptyList.any { true }}") // false
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T> Iterable<T>.any(): Boolean

Returns true if collection has at least one element.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val emptyList = emptyList<Int>()
println("emptyList.any() is ${emptyList.any()}") // false

val nonEmptyList = listOf(1, 2, 3)
println("nonEmptyList.any() is ${nonEmptyList.any()}") // true
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <K, V> Map<out K, V>.any(): Boolean

Returns true if map has at least one entry.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val emptyList = emptyList<Int>()
println("emptyList.any() is ${emptyList.any()}") // false

val nonEmptyList = listOf(1, 2, 3)
println("nonEmptyList.any() is ${nonEmptyList.any()}") // true
//sampleEnd
}
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
inline fun <K, V> Map<out K, V>.any(
    predicate: (Entry<K, V>) -> Boolean
): Boolean

Returns true if at least one entry matches the given predicate.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val isEven: (Int) -> Boolean = { it % 2 == 0 }
val zeroToTen = 0..10
println("zeroToTen.any { isEven(it) } is ${zeroToTen.any { isEven(it) }}") // true
println("zeroToTen.any(isEven) is ${zeroToTen.any(isEven)}") // true

val odds = zeroToTen.map { it * 2 + 1 }
println("odds.any { isEven(it) } is ${odds.any { isEven(it) }}") // false

val emptyList = emptyList<Int>()
println("emptyList.any { true } is ${emptyList.any { true }}") // false
//sampleEnd
}

© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/any.html