Kotlin Collection Functions
Kotlin's standard library includes a rich set of functions for working with collections. These functions process lists, sets, and maps in a concise and readable way — without writing manual loops for most common tasks.
Transforming: map
val prices = listOf(100, 250, 80, 400, 150)
val withTax = prices.map { it * 1.18 }
println(withTax) // [118.0, 295.0, 94.4, 472.0, 177.0]
val asStrings = prices.map { "₹$it" }
println(asStrings) // [₹100, ₹250, ₹80, ₹400, ₹150]Filtering: filter
val ages = listOf(12, 25, 17, 30, 16, 45, 19)
val adults = ages.filter { it >= 18 }
println(adults) // [25, 30, 45, 19]
val minors = ages.filterNot { it >= 18 }
println(minors) // [12, 17, 16]Flattening: flatMap
val groups = listOf(listOf(1, 2, 3), listOf(4, 5), listOf(6, 7, 8, 9))
val all = groups.flatten()
println(all) // [1, 2, 3, 4, 5, 6, 7, 8, 9]
val doubled = groups.flatMap { group -> group.map { it * 2 } }
println(doubled) // [2, 4, 6, 8, 10, 12, 14, 16, 18]Grouping: groupBy
val names = listOf("Alice", "Bob", "Anna", "Brian", "Charlie", "Beth")
val byFirstLetter = names.groupBy { it.first() }
println(byFirstLetter)
// {A=[Alice, Anna], B=[Bob, Brian, Beth], C=[Charlie]}Sorting
val numbers = listOf(5, 2, 8, 1, 9, 3)
println(numbers.sorted()) // [1, 2, 3, 5, 8, 9]
println(numbers.sortedDescending()) // [9, 8, 5, 3, 2, 1]
data class Person(val name: String, val age: Int)
val people = listOf(Person("Bob", 30), Person("Alice", 25), Person("Carol", 28))
val byAge = people.sortedBy { it.age }
val byName = people.sortedBy { it.name }
println(byAge.map { it.name }) // [Alice, Carol, Bob]
println(byName.map { it.name }) // [Alice, Bob, Carol]Aggregation
val values = listOf(10, 20, 30, 40, 50)
println(values.sum()) // 150
println(values.average()) // 30.0
println(values.min()) // 10
println(values.max()) // 50
println(values.count()) // 5
println(values.count { it > 25 }) // 3fold and reduce
val nums = listOf(1, 2, 3, 4, 5)
// reduce: combines elements (no initial value)
val product = nums.reduce { acc, n -> acc * n }
println(product) // 120 (1×2×3×4×5)
// fold: combines with an initial value
val sumFrom100 = nums.fold(100) { acc, n -> acc + n }
println(sumFrom100) // 115 (100+1+2+3+4+5)Testing Conditions
val scores = listOf(80, 92, 75, 88, 60)
println(scores.any { it >= 90 }) // true (at least one)
println(scores.all { it >= 60 }) // true (every score passes)
println(scores.none { it < 50 }) // true (no failures)
println(scores.find { it > 85 }) // 92 (first match)
println(scores.findLast { it > 75 }) // 88 (last match)Partitioning
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val (evens, odds) = numbers.partition { it % 2 == 0 }
println("Evens: $evens") // Evens: [2, 4, 6, 8, 10]
println("Odds: $odds") // Odds: [1, 3, 5, 7, 9]Practical Example: Employee Analysis
data class Employee(val name: String, val dept: String, val salary: Int)
fun main() {
val staff = listOf(
Employee("Alice", "Tech", 95000),
Employee("Bob", "Sales", 62000),
Employee("Carol", "Tech", 88000),
Employee("Dan", "HR", 55000),
Employee("Emma", "Sales", 71000),
Employee("Frank", "Tech", 102000)
)
val byDept = staff.groupBy { it.dept }
byDept.forEach { (dept, members) ->
val avgSalary = members.map { it.salary }.average()
println("$dept: ${members.size} people, avg salary ₹${"%.0f".format(avgSalary)}")
}
val topEarner = staff.maxByOrNull { it.salary }
println("Top earner: ${topEarner?.name} at ₹${topEarner?.salary}")
}Output:
Tech: 3 people, avg salary ₹95000
Sales: 2 people, avg salary ₹66500R: 1 people, avg salary ₹55000
Top earner: Frank at ₹102000