🐦 Kotlin
Intermediate
What is the filter function in Kotlin collections?
Answer
The filter function returns a new list containing only the elements that satisfy a given predicate (a lambda returning Boolean). val adults = users.filter { it.age >= 18 }. The original collection is not modified. If no elements match, an empty list is returned (never null). Related functions: filterNot { } keeps elements that do NOT match the predicate, filterIsInstance<T>() filters by type and smart-casts the results, and filterNotNull() removes null elements from a nullable collection. All these functions create intermediate lists — use Sequences for chaining multiple operations efficiently.
Previous
What is a reified type parameter in Kotlin?
Next
What does the map function do in Kotlin collections?