liveBook · Manning

Data transformation

As explained in Kotlin in Action, 2E

map is a Kotlin standard-library operation that transforms each element and collects the resulting values into a new collection.

`map` transforms every element of a collection by applying a function and collecting its return values into a new collection. The output keeps the input’s element count, but its element type or representation can change. Use it instead of manual iteration when each item needs the same conversion. section 6.1.1 section 6.1

Square every number

```kotlin val numbers = listOf(1, 2, 3, 4) println(numbers.map { it * it }) // [1, 4, 9, 16] ``` The lambda receives each element as `it`; its result becomes the corresponding element of the new list. `map` transforms rather than removes values, unlike `filter`, which retains only elements whose predicate is true. section 6.1.1

fun main() {
   val list = listOf(1, 2, 3, 4)
   println(list.map { it * it })
   // [1, 4, 9, 16]
}

Follow the input-to-output transformation: each number is replaced by its square in a newly collected result. section 6.1.1

Transforming objects is a common use: `people.map { it.name }` produces names rather than `Person` objects. A member reference can make the same operation shorter: `people.map(Person::name)`. Calls can be chained, such as filtering people by age before mapping the remaining people to names. section 6.1.1 section 1.2.3

Use related collection operations according to the result needed:

  • Use `filter` to select matching elements without changing their type. section 6.1.1
  • Use `mapIndexed` when the transformation needs both an element and its index. section 6.1.1
  • Use `mapValues` to transform values in a map while retaining its keys. section 6.1.1
  • Use `flatMap` for nested collections when transformation and flattening belong in one step. section 6.3

`map` belongs to a shared vocabulary of Kotlin collection operations used with lambdas. It works with the same Java collection classes Kotlin uses on the JVM, so collection values can be exchanged with Java code without conversion. For chains with many operations, sequences can avoid creating intermediate collections while using the same operations. section 3.1 section 6.3

How Kotlin in Action, 2E covers it

How deep it goes
Defines element-wise transformation, contrasts filtering, and demonstrates lambdas, member references, chaining, indexed mapping, and map values.
Examples
Demonstrates squaring numbers, extracting Person names, filtering by age before mapping, indexed mapping, and uppercasing map values.
What it assumes
Uses collections and lambda expressions while introducing map through number and Person examples.
What you can do after
Apply it via the Kotlin standard library to replace manual element-by-element collection transformations.
How it connects
Connects map with filter, lambda expressions, member references, sequences, flatMap, and Java collections.

Also explained in

Compare how 20 books explain data transformation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.