Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

map

map extension function enables you to apply a transformation to all elements in a collection. It takes a transformer function as a lambda-parameter.

fun main() {

    val numbers = listOf(1, -2, 3, -4, 5, -6)     // 1
    
    val doubled = numbers.map { x -> x * 2 }      // 2
    
    val tripled = numbers.map { it * 3 }          // 3

    println("Numbers: $numbers")
    println("Doubled Numbers: $doubled")
    println("Tripled Numbers: $tripled")
}
  1. Defines a collection of numbers.
  2. Doubles numbers.
  3. Uses the shorter it notation to triple the numbers.