In Kotlin, the main collection types are List, Set, and Map.
Kotlin provides both read-only and mutable versions:
| Collection | Read-only | Mutable |
|---|---|---|
| List | List<T> |
MutableList<T> |
| Set | Set<T> |
MutableSet<T> |
| Map | Map<K, V> |
MutableMap<K, V> |
Lists
A list is an ordered collection. It can contain duplicate elements.
Read-only list
val numbers = listOf(1, 2, 3, 3)
println(numbers[0]) // 1
println(numbers.size) // 4
println(numbers.contains(2)) // true
You cannot add or remove items from a read-only List.
val names = listOf("Alice", "Bob", "Charlie")
for (name in names) {
println(name)
}
Mutable list
val names = mutableListOf("Alice", "Bob")
names.add("Charlie")
names.remove("Alice")
names[0] = "Bobby"
println(names) // [Bobby, Charlie]
You can also create an empty mutable list:
val items = mutableListOf<String>()
items.add("Book")
items.add("Pen")
println(items) // [Book, Pen]
Sets
A set is a collection of unique elements. It does not allow duplicates.
Read-only set
val numbers = setOf(1, 2, 3, 3)
println(numbers) // [1, 2, 3]
println(2 in numbers) // true
Mutable set
val fruits = mutableSetOf("Apple", "Banana")
fruits.add("Orange")
fruits.add("Apple") // Duplicate, ignored
fruits.remove("Banana")
println(fruits) // [Apple, Orange]
Empty mutable set:
val ids = mutableSetOf<Int>()
ids.add(101)
ids.add(102)
println(ids) // [101, 102]
Maps
A map stores key-value pairs. Each key is unique.
Read-only map
val ages = mapOf(
"Alice" to 25,
"Bob" to 30,
"Charlie" to 35
)
println(ages["Alice"]) // 25
println(ages["Unknown"]) // null
println(ages.containsKey("Bob")) // true
println(ages.containsValue(30)) // true
Mutable map
val scores = mutableMapOf(
"Alice" to 90,
"Bob" to 85
)
scores["Charlie"] = 95
scores["Alice"] = 100
scores.remove("Bob")
println(scores) // {Alice=100, Charlie=95}
Empty mutable map:
val phoneBook = mutableMapOf<String, String>()
phoneBook["Alice"] = "123-456"
phoneBook["Bob"] = "987-654"
println(phoneBook["Alice"]) // 123-456
Common operations
Iterating over a list or set
val colors = listOf("Red", "Green", "Blue")
for (color in colors) {
println(color)
}
Iterating over a map
val ages = mapOf(
"Alice" to 25,
"Bob" to 30
)
for ((name, age) in ages) {
println("$name is $age years old")
}
Filtering
val numbers = listOf(1, 2, 3, 4, 5, 6)
val evenNumbers = numbers.filter { it % 2 == 0 }
println(evenNumbers) // [2, 4, 6]
Mapping values
val names = listOf("alice", "bob", "charlie")
val uppercaseNames = names.map { it.uppercase() }
println(uppercaseNames) // [ALICE, BOB, CHARLIE]
Sorting
val numbers = listOf(5, 2, 8, 1)
val sorted = numbers.sorted()
println(sorted) // [1, 2, 5, 8]
Checking contents
val names = listOf("Alice", "Bob")
println("Alice" in names) // true
println("Charlie" !in names) // true
Choosing between them
Use a List when:
- Order matters
- Duplicates are allowed
- You access elements by index
val tasks = listOf("Write", "Test", "Deploy")
Use a Set when:
- Values must be unique
- You mainly check whether something exists
val uniqueTags = setOf("kotlin", "backend", "api")
Use a Map when:
- You need key-value lookup
- Each key maps to one value
val userRoles = mapOf(
1 to "Admin",
2 to "Editor",
3 to "Viewer"
)
Quick summary
val readOnlyList = listOf("A", "B", "C")
val mutableList = mutableListOf("A", "B")
mutableList.add("C")
val readOnlySet = setOf("A", "B", "A") // [A, B]
val mutableSet = mutableSetOf("A", "B")
mutableSet.add("C")
val readOnlyMap = mapOf("Alice" to 25, "Bob" to 30)
val mutableMap = mutableMapOf("Alice" to 25)
mutableMap["Bob"] = 30
In short:
listOf()creates a read-only listmutableListOf()creates a mutable listsetOf()creates a read-only setmutableSetOf()creates a mutable setmapOf()creates a read-only mapmutableMapOf()creates a mutable map
