Kotlin Academy · Lesson

mutableListOf, mutableMapOf: Mutable Builders

Create mutable collections and modify them safely.

Lesson 2 of 413 steps

mutableListOf, mutableMapOf: Mutable Builders is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

When You Need Mutation

Mutable collections allow adding, removing, and updating elements. Use them when the collection grows or changes over time.

mutableListOf

mutableListOf creates a MutableList backed by ArrayList. You can add, remove, and replace elements.
val list = mutableListOf("a", "b", "c")
list.add("d")
list.add(0, "z")  // insert at index 0
list.remove("b")
println(list)  // [z, a, c, d]

mutableSetOf

mutableSetOf creates a MutableSet. add returns false if the element already exists.
val set = mutableSetOf(1, 2, 3)
set.add(4)
set.add(2)  // already exists, no change
set.remove(1)
println(set)  // [2, 3, 4]

mutableMapOf

mutableMapOf creates a MutableMap. Use put or [] for insertion and update.
val map = mutableMapOf("a" to 1, "b" to 2)
map["c"] = 3
map["a"] = 10      // update
map.remove("b")
println(map)  // {a=10, c=3}

addAll, removeAll, retainAll

Bulk operations for mutable collections.
val nums = mutableListOf(1, 2, 3)
nums.addAll(listOf(4, 5, 6))
println(nums)  // [1, 2, 3, 4, 5, 6]
nums.removeAll { it % 2 == 0 }
println(nums)  // [1, 3, 5]

clear and replaceAll

clear removes all elements. replaceAll transforms each element in place.
val list = mutableListOf(1, 2, 3, 4)
list.replaceAll { it * 2 }
println(list)  // [2, 4, 6, 8]
list.clear()
println(list)  // []

Building a List with buildList

buildList creates a read-only list via a mutable builder — clean and concise.
val squares = buildList {
    for (i in 1..5) add(i * i)
}
println(squares)  // [1, 4, 9, 16, 25]

buildMap and buildSet

Similarly, buildMap and buildSet let you build maps and sets using mutable operations, returning read-only results.
val map = buildMap {
    put("en", "English")
    put("de", "German")
    put("tr", "Turkish")
}
println(map["tr"])  // Turkish

ArrayList, LinkedList: Explicit Types

If you need specific performance characteristics, use ArrayList or ArrayDeque directly.
val arrayList = ArrayList<String>()
arrayList.add("x")
val deque = ArrayDeque<Int>()
deque.addFirst(1)
deque.addLast(2)

getOrPut for Map Accumulation

getOrPut returns an existing value or inserts a default and returns it — great for grouping.
val groups = mutableMapOf<String, MutableList<Int>>()
for (i in 1..6) {
    val key = if (i % 2 == 0) "even" else "odd"
    groups.getOrPut(key) { mutableListOf() }.add(i)
}
println(groups)

Sorting Mutable Lists

sort and sortWith sort mutable lists in place.
val nums = mutableListOf(3, 1, 4, 1, 5, 9)
nums.sort()
println(nums)  // [1, 1, 3, 4, 5, 9]
nums.sortDescending()
println(nums)  // [9, 5, 4, 3, 1, 1]

Quick Check

Which function creates a read-only List using a mutable builder inside a lambda?

Recap

mutableListOf/SetOf/MapOf for mutable collections. add, remove, put, clear, replaceAll for mutations. buildList/Map/Set for clean immutable results from mutable building. Next: accessing elements safely!
Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “mutableListOf, mutableMapOf: Mutable Builders” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “mutableListOf, mutableMapOf: Mutable Builders”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

What will I learn in “mutableListOf, mutableMapOf: Mutable Builders”?

Create mutable collections and modify them safely. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “mutableListOf, mutableMapOf: Mutable Builders” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. listOf, setOf, mapOf: Read-Only Collections
  2. mutableListOf, mutableMapOf: Mutable Builders
  3. Accessing Elements: get, first, last, getOrNull
  4. Iterating and Transforming Collections with Standard Library
← Back to Kotlin Academy