Kotlin has rich collection APIs with functional operations like
map, filter, reduce, and grouping.
Lists
Kotlin
val names = listOf("Ram", "Sita", "Hari") // immutable
val mutable = mutableListOf("Ram") // mutable
mutable.add("Sita")
// Operations
val upper = names.map { it.uppercase() } // ["RAM", "SITA", "HARI"]
val short = names.filter { it.length <= 3 } // ["Ram"]
val found = names.find { it.startsWith("S") } // "Sita"
val sorted = names.sorted() // ["Hari", "Ram", "Sita"]Maps
Kotlin
val scores = mapOf(
"Ram" to 85,
"Sita" to 92,
"Hari" to 78
)
println(scores["Ram"]) // 85
println(scores.getOrDefault("Gita", 0)) // 0
val upper = scores.mapValues { (_, v) -> v + 5 }
// {Ram=90, Sita=97, Hari=83}Sets
Kotlin
val unique = setOf(1, 2, 3, 2, 1) // {1, 2, 3}
val a = setOf(1, 2, 3)
val b = setOf(3, 4, 5)
println(a union b) // {1, 2, 3, 4, 5}
println(a intersect b) // {3}Functional Operations
Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
numbers.filter { it % 2 == 0 } // [2, 4]
numbers.map { it * it } // [1, 4, 9, 16, 25]
numbers.reduce { acc, n -> acc + n } // 15
numbers.take(3) // [1, 2, 3]
numbers.drop(2) // [3, 4, 5]
numbers.any { it > 3 } // true
numbers.all { it > 0 } // trueGrouping
Kotlin
val students = listOf("Ram" to "A", "Sita" to "B", "Hari" to "A")
val grouped = students.groupBy { it.second }
// {A=[Ram, A], B=[Sita, B]}
val partitioned = numbers.partition { it > 3 }
// Pair([4, 5], [1, 2, 3])Tips
- Use immutable collections by default (
listOf,mapOf). - Chain operations for readable data pipelines:
list.filter {...}.map {...}. - Use
groupingfor aggregations. - Kotlin collections are null-safe — no NPE from missing keys.
🎉 You have completed the Kotlin course! You now know null safety, data classes, coroutines, and collections — the core skills for Android and JVM development.