W3cubDocs

/Kotlin

any

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T> Sequence<T>.any(): Boolean

Returns true if sequence has at least one element.

The operation is terminal.

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> Sequence<T>.any(
    predicate: (T) -> Boolean
): Boolean

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

The operation is terminal.

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–2020 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.sequences/any.html