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
mutableListOf
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
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
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
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
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
val squares = buildList {
for (i in 1..5) add(i * i)
}
println(squares) // [1, 4, 9, 16, 25]buildMap and buildSet
val map = buildMap {
put("en", "English")
put("de", "German")
put("tr", "Turkish")
}
println(map["tr"]) // TurkishArrayList, LinkedList: Explicit Types
val arrayList = ArrayList<String>()
arrayList.add("x")
val deque = ArrayDeque<Int>()
deque.addFirst(1)
deque.addLast(2)getOrPut for Map Accumulation
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
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
Recap
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
- listOf, setOf, mapOf: Read-Only Collections
- mutableListOf, mutableMapOf: Mutable Builders
- Accessing Elements: get, first, last, getOrNull
- Iterating and Transforming Collections with Standard Library