liveBook · Manning

List

As explained in Kotlin in Action, 2E

MutableList is a Kotlin mutable collection type whose elements can be read and modified through its invariant type parameter.

A Kotlin list keeps elements in order. Use List when callers only need to read values, and MutableList when code must add, replace, or otherwise change them. This split makes mutation visible in an API while retaining direct compatibility with Java collection classes. section 3.1 section 8.2.3

Create and update a list

```kotlin val names = mutableListOf("Martin", "Samuel") names.replaceAll { it.uppercase() } // [MARTIN, SAMUEL] names.fill("(redacted)") // [(redacted), (redacted)] ``` `mutableListOf` creates a mutable list. `replaceAll` computes a replacement for every element; `fill` is the shorter choice when every element receives the same value. section 6.1.7

Choose the interface from the operations a function needs:

  • Accept `List<T>` to inspect elements without exposing mutating operations. Read-only lists support operations such as `last()`, `shuffled()`, and numeric `sum()`. section 3.1
  • Accept `MutableList<T>` when the function must add or replace elements. A mutable interface also extends its corresponding read-only interface. section 8.2.3
  • Prefer collections to arrays by default; Kotlin and Java collection values can be passed across the language boundary without conversion. section 8.2.4

Mutability matters to generic type safety. A `List<String>` can be passed where `List<Any>` is expected because reading strings as `Any` is safe. A `MutableList<String>` cannot be passed as `MutableList<Any>`: code receiving it could insert an integer and corrupt the list’s promised element type. section 11.3.1 section 11.4

Model missing parsed values

```kotlin fun readNumbers(text: String): List<Int?> { val result = mutableListOf<Int?>() for (line in text.lineSequence()) { result.add(line.toIntOrNull()) } return result } ``` `List<Int?>` means the list exists but individual entries may be absent. This differs from `List<Int>?`, where the list reference itself may be absent. Use `filterNotNull()` when later work needs only valid, non-null values. section 8.2.1

Java declarations may not state whether a collection is nullable, whether its elements are nullable, or whether callers may mutate it. When implementing such APIs, select the Kotlin type that matches the actual contract—for example, `MutableList<Person>` for output and `MutableList<String?>` for optional per-item errors. section 8.2.4

Table 8.2 Collection-creation functions (view table figure)

Collection type

Read-only

Mutable

List

listOf, List

mutableListOf, MutableList, arrayListOf, buildList

Set

setOf

mutableSetOf, hashSetOf, linkedSetOf, sortedSetOf, buildSet

Map

mapOf

mutableMapOf, hashMapOf, linkedMapOf, sortedMapOf, buildMap

Compare the factory functions for read-only and mutable lists alongside Kotlin’s corresponding set and map types. section 8.2.3

How Kotlin in Action, 2E covers it

How deep it goes
Explains creation, mutation, nullability, Java interoperation, and variance-based safety without detailing collection implementations.
Examples
Uses name replacement, line-by-line number parsing, Java parser interfaces, and unsafe mixed-type mutation scenarios.
What it assumes
Builds from basic collection creation and introduces nullable type arguments before applying them to lists.
What you can do after
Apply it via Kotlin collection factories and operations while choosing mutability and nullability that match API contracts.
How it connects
Connects List and MutableList with generics, variance, nullability, Java collections, arrays, and collection operations.

Also explained in

Compare how 21 books explain list →

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.