any

fun <T> Array<out T>.any(): Boolean(source)

Returns true if array has at least one element.

Since Kotlin

1.0

Samples

import kotlin.test.*

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

inline fun <T> Array<out T>.any(predicate: (T) -> Boolean): Boolean(source)
inline fun ByteArray.any(predicate: (Byte) -> Boolean): Boolean(source)
inline fun ShortArray.any(predicate: (Short) -> Boolean): Boolean(source)
inline fun IntArray.any(predicate: (Int) -> Boolean): Boolean(source)
inline fun LongArray.any(predicate: (Long) -> Boolean): Boolean(source)
inline fun FloatArray.any(predicate: (Float) -> Boolean): Boolean(source)
inline fun DoubleArray.any(predicate: (Double) -> Boolean): Boolean(source)
inline fun BooleanArray.any(predicate: (Boolean) -> Boolean): Boolean(source)
inline fun CharArray.any(predicate: (Char) -> Boolean): Boolean(source)
inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean): Boolean(source)

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

Since Kotlin

1.0

Samples

import kotlin.test.*

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

Returns true if collection has at least one element.

Since Kotlin

1.0

Samples

import kotlin.test.*

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

fun <K, V> Map<out K, V>.any(): Boolean(source)

Returns true if map has at least one entry.

Since Kotlin

1.0

Samples

import kotlin.test.*

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

inline fun <K, V> Map<out K, V>.any(predicate: (Map.Entry<K, V>) -> Boolean): Boolean(source)

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

Since Kotlin

1.0

Samples

import kotlin.test.*

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

Returns true if array has at least one element.

Since Kotlin

1.3

Samples

import kotlin.test.*

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

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

Since Kotlin

1.3

Samples

import kotlin.test.*

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