In this post, we demonstrate the usage of all() method in Kotlin with an example. The all() returns true if all elements satisfy the given predicate function.
Kotlin List all Example
The following example shows the usage of all() method in Kotlin.
package net.sourcecodeexamples.kotlin fun main() { val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9) val nums2 = listOf(-3, -4, -2, -5, -7, -8) // testing for positive only values val r = nums.all { e - > e > 0 } if (r) { println("nums list contains only positive values") } else { println("nums list does not contain only positive values") } // testing for negative only values val r2 = nums2.all { e - > e < 0 } if (r2) { println("nums2 list contains only negative values") } else { println("nums2 list does not contain only negative values") } }
Output:
nums list contains only positive values
nums2 list contains only negative values
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course