Kotlin filtering arrays example



In this Kotlin example, we will discuss how to filter arrays in Kotlin with an example.
With the filter() method, we can filter data in an array.
All Kotlin examples at All Kotlin Examples.

Kotlin filtering arrays example

The example creates an array of positive and negative integers. The filter() method is used to pick up only positive values.
fun main() {

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

    nums.filter { e -> e > 0 }.forEach { e -> print("$e ") }
}
Output:
1 3 4 7 
All Kotlin examples at All Kotlin Examples.

Comments