Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 23 / 68

Transforming a List (map)

In this lesson you will learn how to transform a list with map in Kotlin, so you can process every element in one go. This is for anyone searching for "Kotlin map how to use".

.map { } applies the same operation to every element and produces a new list. Think of it as a machine on a conveyor belt that processes every product that passes through it in the same way.

The sample code builds a new list doubled with every element of the original [1, 2, 3] doubled, using nums.map { it * 2 }. The it shorthand refers to a lambda's single parameter - a distinctly Kotlin notation.

A common stumbling block for beginners is that map returns a new list rather than changing the original. Compared with writing a for loop element by element, what you are trying to do shows up directly in the code.

In real-world development, combining it with relatives such as filter and reduce expresses common data-shaping work very concisely.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Kotlin directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)