mutableListOf, mutableMapOf: Mutable Builders
Create mutable collections and modify them safely.
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
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
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
- listOf, setOf, mapOf: Read-Only Collections
- mutableListOf, mutableMapOf: Mutable Builders
- Accessing Elements: get, first, last, getOrNull
- Iterating and Transforming Collections with Standard Library