Kotlin Collection Functions — Complete Guide

1. What Are Collection Functions in Kotlin?

Kotlin provides a rich set of collection functions to perform operations on Lists, Sets, and Maps. These functions make it easier to filter, transform, search, sort, and aggregate data in a concise and readable way.

2. Types of Collection Functions


3. Examples of Collection Functions

Example 1 — Filtering a List

val numbers = listOf(1, 2, 3, 4, 5, 6)

// Keep even numbers only
val evenNumbers = numbers.filter { it % 2 == 0 }
println(evenNumbers) // [2, 4, 6]

// Remove null values
val items = listOf("A", null, "B")
val nonNullItems = items.filterNotNull()
println(nonNullItems) // [A, B]
        
Example 2 — Mapping / Transformation

val names = listOf("Ali", "Ahmed", "Zain")

// Convert to uppercase
val upperNames = names.map { it.uppercase() }
println(upperNames) // [ALI, AHMED, ZAIN]

// Multiply numbers by 2
val doubled = numbers.map { it * 2 }
println(doubled) // [2, 4, 6, 8, 10, 12]
        
Example 3 — Searching / Querying

// Find first number > 3
val first = numbers.find { it > 3 }
println(first) // 4

// Check if list contains 5
println(numbers.contains(5)) // true
        
Example 4 — Aggregation / Reduction

// Sum of numbers
println(numbers.sum()) // 21

// Count elements greater than 3
println(numbers.count { it > 3 }) // 3

// Reduce to product of numbers
val product = numbers.reduce { acc, num -> acc * num }
println(product) // 720

// Fold with initial value
val sumWithInitial = numbers.fold(10) { acc, num -> acc + num }
println(sumWithInitial) // 31
        
Example 5 — Sorting

val unsorted = listOf(5, 2, 8, 1)

// Sort ascending
println(unsorted.sorted()) // [1, 2, 5, 8]

// Sort descending
println(unsorted.sortedDescending()) // [8, 5, 2, 1]
        
Example 6 — Grouping / Partitioning

val words = listOf("apple", "banana", "apricot", "cherry")

// Group by first letter
val grouped = words.groupBy { it.first() }
println(grouped) 
// {a=[apple, apricot], b=[banana], c=[cherry]}

// Partition numbers into even and odd
val (evens, odds) = numbers.partition { it % 2 == 0 }
println(evens) // [2, 4, 6]
println(odds)  // [1, 3, 5]
        
Example 7 — Other Useful Functions

val nums = listOf(1, 2, 2, 3, 4)

// Remove duplicates
println(nums.distinct()) // [1, 2, 3, 4]

// Take first 3 elements
println(nums.take(3)) // [1, 2, 2]

// Drop first 2 elements
println(nums.drop(2)) // [2, 3, 4]

// Chunked into pairs
println(nums.chunked(2)) // [[1, 2], [2, 3], [4]]

// Check conditions
println(nums.all { it > 0 }) // true
println(nums.any { it > 3 }) // true
println(nums.none { it < 0 }) // true
        

4. When to Use Collection Functions?

5. Top 8 Interview Questions (With Answers)

  1. Q1: What is the difference between map and forEach?
    Ans: map transforms elements and returns a new collection; forEach just performs an action on each element.
  2. Q2: How do you filter null values from a collection?
    Ans: Use filterNotNull().
  3. Q3: How do you combine multiple collection functions?
    Ans: You can chain them, e.g., numbers.filter { it > 2 }.map { it * 2 }.
  4. Q4: Can collection functions be used on Sets and Maps?
    Ans: Yes! Most functions work on any collection. For Maps, you can use mapKeys, mapValues, or convert to a List.
  5. Q5: Difference between reduce and fold?
    Ans: reduce uses the first element as initial accumulator; fold allows specifying a custom initial value.
  6. Q6: How to check if all elements satisfy a condition?
    Ans: Use all { condition }.
  7. Q7: How to get first element matching a condition?
    Ans: Use first { condition } or find { condition }.
  8. Q8: How to partition a collection?
    Ans: Use partition { condition } to split into two lists.
Bonus Tip 💡

Kotlin collection functions make your code concise, expressive, and easy to maintain. For Android, you can process lists or API data without loops, making your code cleaner and less error-prone.