filter, map, and find on Collections
Apply lambdas to transform and filter collections using standard library functions.
Three Workhorse Functions
filter keeps elements matching a predicate. map transforms each element. find returns the first matching element (or null). Together they cover the bulk of collection processing.
filter Basics
filter returns a new list containing only elements for which the predicate is true.
fun main() {
val nums = listOf(1, 2, 3, 4, 5, 6)
val evens = nums.filter { it % 2 == 0 }
println(evens) // [2, 4, 6]
}All lessons in this course
- What Is a Lambda? Syntax and Types
- Passing Lambdas to Functions
- it: The Implicit Parameter
- filter, map, and find on Collections