Functional constructs for iteration

suggest change

The Kotlin Standard Library also provides numerous useful functions to iteratively work upon collections.

For example, the map function can be used to transform a list of items.

val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
val numberStrings = numbers.map { "Number $it" }

One advantage of this style is that operations can be chained. Only a small change is required if, say, the list above should be filtered to even numbers. The filter function does that.

val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
val numberStrings = numbers.filter { it % 2 == 0 }.map { "Number $it" }

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents