Collections, Arrays, Lists, Sets, Maps, and Transformations
Master Kotlin’s collection model and learn how to transform, filter, aggregate, and restructure data efficiently.
Inside this chapter
- Why Collections Matter So Much
- Common Collection Types
- Transformations
- Mutable vs Read-Only Views
- Sequence Pipelines
- Real-Time Example
Series navigation
Study the chapters in order for the clearest path from Kotlin setup and syntax to coroutines, backend work, clean design, multiplatform thinking, and advanced engineering practice. Use the navigation at the bottom to move smoothly through the full tutorial series.
Why Collections Matter So Much
Most business software processes collections of data: users, orders, products, records, logs, messages, reports, transactions, or configuration items. Kotlin’s collections APIs are one of its strongest productivity features.
Common Collection Types
val names = listOf("Anu", "Rita", "Karan")
val ids = setOf(10, 20, 30)
val scores = mapOf("Math" to 95, "Science" to 91)
Lists preserve order, sets help avoid duplicates, and maps associate keys with values. Choosing the right structure matters for both correctness and readability.
Transformations
val prices = listOf(100, 250, 90, 400)
val premium = prices.filter { it > 100 }
val labels = premium.map { "INR $it" }
Functions such as map, filter, groupBy, associateBy, and reduce are used constantly in real code.
Mutable vs Read-Only Views
val readOnly = listOf(1, 2, 3)
val mutable = mutableListOf(1, 2, 3)
mutable.add(4)
Being intentional about mutability improves reasoning and helps avoid accidental state changes.
Sequence Pipelines
val result = (1..1000)
.asSequence()
.filter { it % 2 == 0 }
.map { it * it }
.take(5)
.toList()
Sequences can improve efficiency for chained lazy operations on larger datasets.
Real-Time Example
A retail analytics service may group orders by city, filter high-value carts, map internal records to API response models, and compute summary statistics. Good collection fluency makes such tasks much faster to implement and review.