Kotlin List any Example

In this post, we demonstrate the usage of any() method with an example. The any() method returns true if at least one element matches the given predicate function.

Kotlin List any Example

The following example shows the usage of any() method in Kotlin.
package net.sourcecodeexamples.kotlin

fun main() {

 val nums = listOf(1,2,3,4,5,6,7,-1,-2)

 val r = nums.any { e -> e > 10 }
 if (r) {
  println("There is a value greater than ten")
 } else {
  println("There is no value greater than ten")
 }

 val r2 = nums.any { e -> e < 0 }

 if (r2) {
  println("There is a negative value")
 } else {
  println("There is no negative value")
 }
}

Output:
There is no value greater than ten
There is a negative value


Comments