August 2017
Intermediate to advanced
440 pages
10h
English
We have already briefly presented map, filter, and flatMap, because they are the most basic stream processing functions. The map function returns a list with elements changed according to the function from the argument:
val list = listOf(1,2,3).map { it * 2 } println(list) // Prints: [2, 4, 6]
The filter function allows only the elements that match the provided predicate:
val list = listOf(1,2,3,4,5).map { it > 2 } println(list) // Prints: [3, 4, 5]
The flatMap function returns a single list of all elements yielded by the transform function, which is invoked on each element of the original collection:
val list = listOf(10, 20).flatMap { listOf(it, it+1, it + 2) } println(list) // Prints: [10, 11, ...Read now
Unlock full access