0Pricing
Kotlin Academy · Lesson

mutableListOf, mutableMapOf: Mutable Builders

Create mutable collections and modify them safely.

mutableListOf, mutableMapOf: Mutable Builders is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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!

Frequently asked questions

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

Yes — the full text of “mutableListOf, mutableMapOf: Mutable Builders” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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