0
0
KotlinHow-ToBeginner · 3 min read

How to Map List in Kotlin: Simple Guide with Examples

In Kotlin, you can transform a list using the map function, which applies a given transformation to each element and returns a new list with the results. Use list.map { element -> transformation } to create a new list based on the original.
📐

Syntax

The map function takes a lambda expression that defines how each element in the list should be transformed. It returns a new list containing the transformed elements.

  • list: The original list you want to transform.
  • map { element -> ... }: The function that applies the transformation to each element.
  • The lambda parameter element represents each item in the list.
  • The result of the lambda is collected into a new list.
kotlin
val originalList = listOf(1, 2, 3)
val mappedList = originalList.map { number -> number * 2 }
💻

Example

This example shows how to double each number in a list using map. The original list remains unchanged, and a new list with doubled values is created.

kotlin
fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val doubled = numbers.map { it * 2 }
    println("Original list: $numbers")
    println("Doubled list: $doubled")
}
Output
Original list: [1, 2, 3, 4, 5] Doubled list: [2, 4, 6, 8, 10]
⚠️

Common Pitfalls

One common mistake is trying to modify the original list directly, but map always returns a new list and does not change the original. Another is forgetting to return a value inside the lambda, which leads to unexpected results.

Also, using map when you only want to perform an action without creating a new list is inefficient; use forEach instead.

kotlin
fun main() {
    val numbers = listOf(1, 2, 3)
    // Wrong: map used without returning a value
    val wrong = numbers.map {
        println(it) // prints but returns Unit, so list is [Unit, Unit, Unit]
    }
    println(wrong) // Output: [kotlin.Unit, kotlin.Unit, kotlin.Unit]

    // Right: return transformed value
    val right = numbers.map { it * 3 }
    println(right) // Output: [3, 6, 9]
}
Output
1 2 3 [kotlin.Unit, kotlin.Unit, kotlin.Unit] [3, 6, 9]
📊

Quick Reference

Use map to transform each element of a list and get a new list. Remember:

  • Input: Original list
  • Output: New list with transformed elements
  • Lambda: Defines how to change each element
  • Original list: Remains unchanged
ConceptDescription
list.map { element -> ... }Transforms each element and returns a new list
Lambda parameterRepresents each item in the list during transformation
ReturnsA new list with transformed elements
Original listNot modified by map
Use forEachWhen you want to perform actions without creating a new list

Key Takeaways

Use map to create a new list by transforming each element of the original list.
The original list stays unchanged after using map.
Always return a value inside the lambda to get the expected transformed list.
Use forEach if you only want to perform actions without creating a new list.
The lambda parameter represents each element and defines how it changes.