Using the 'any' Function for Existence Checks

The 'any' function in Kotlin is a powerful tool for checking whether any elements in a collection meet a specific condition. This is particularly useful when you want to quickly determine if at least one item in a list or collection satisfies a given predicate without having to manually iterate through the entire collection. It returns a boolean value, making it ideal for conditional logic.


val numbers = listOf(1, 2, 3, 4, 5)

// Check if any number is greater than 3
val hasGreaterThanThree = numbers.any { it > 3 }

println(hasGreaterThanThree) // Output: true