codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

37 / 68 topics
36Kotlin Standard Library37Collections Functions38Sequences in Kotlin39Date and Time API40Kotlin Serialization41Kotlin Coroutines Library42Kotlin HTML DSL43Kotlin CLI
Tutorials/Kotlin/Collections Functions
🎯Kotlin

Collections Functions

Updated 2026-04-20
3 min read

Introduction

Collections are fundamental data structures in programming, and Kotlin provides a rich set of functions to manipulate them efficiently. This tutorial will explore various collection functions available in Kotlin, including transformations, filtering, sorting, and aggregation operations. We'll cover both mutable and immutable collections, providing real-world examples and best practices.

Understanding Collections in Kotlin

Kotlin supports several types of collections:

  • Lists: Ordered collections that can contain duplicate elements.
  • Sets: Unordered collections that cannot contain duplicate elements.
  • Maps: Key-value pairs where keys are unique.

Collections in Kotlin can be mutable (e.g., MutableList, MutableSet) or immutable (e.g., List, Set). Immutable collections are safer and more predictable, especially in concurrent environments.

Basic Collection Functions

Transformations

map

The map function transforms each element of a collection into another form. It returns a new list containing the transformed elements.

val numbers = listOf(1, 2, 3, 4)
val doubledNumbers = numbers.map { it * 2 }
println(doubledNumbers) // Output: [2, 4, 6, 8]

flatMap

flatMap is similar to map, but it flattens the result into a single list. This is useful when each element maps to a collection.

val listOfLists = listOf(listOf(1, 2), listOf(3, 4))
val flattenedList = listOfLists.flatMap { it }
println(flattenedList) // Output: [1, 2, 3, 4]

Filtering

filter

The filter function returns a new list containing only the elements that satisfy a given predicate.

val numbers = listOf(1, 2, 3, 4, 5)
val evenNumbers = numbers.filter { it % 2 == 0 }
println(evenNumbers) // Output: [2, 4]

filterNot

filterNot returns a list of elements that do not satisfy the predicate.

val oddNumbers = numbers.filterNot { it % 2 == 0 }
println(oddNumbers) // Output: [1, 3, 5]

Sorting

sorted

The sorted function returns a new list containing all elements in ascending order.

val unsortedList = listOf(4, 1, 3, 2)
val sortedList = unsortedList.sorted()
println(sortedList) // Output: [1, 2, 3, 4]

sortedBy

sortedBy sorts the elements based on a specified key.

data class Person(val name: String, val age: Int)

val people = listOf(Person("Alice", 30), Person("Bob", 25))
val sortedPeople = people.sortedBy { it.age }
println(sortedPeople) // Output: [Person(name=Bob, age=25), Person(name=Alice, age=30)]

Aggregation

sumOf

The sumOf function calculates the sum of all elements in a collection.

val numbers = listOf(1, 2, 3, 4)
val sum = numbers.sumOf { it }
println(sum) // Output: 10

averageOf

averageOf computes the average of the elements.

val average = numbers.averageOf { it.toDouble() }
println(average) // Output: 2.5

Advanced Collection Functions

Grouping and Partitioning

groupBy

The groupBy function groups elements by a specified key.

data class Product(val category: String, val price: Int)

val products = listOf(
    Product("Electronics", 100),
    Product("Clothing", 50),
    Product("Electronics", 200)
)

val groupedProducts = products.groupBy { it.category }
println(groupedProducts) // Output: {Electronics=[Product(category=Electronics, price=100), Product(category=Electronics, price=200)], Clothing=[Product(category=Clothing, price=50)]}

partition

partition splits a collection into two lists based on a predicate.

val (evenNumbers, oddNumbers) = numbers.partition { it % 2 == 0 }
println(evenNumbers) // Output: [2, 4]
println(oddNumbers) // Output: [1, 3, 5]

Reduction

reduce

The reduce function reduces a collection to a single value by applying an operation cumulatively.

val product = numbers.reduce { acc, i -> acc * i }
println(product) // Output: 24

fold

fold is similar to reduce, but it allows specifying an initial accumulator value.

val sumWithInitialValue = numbers.fold(10) { acc, i -> acc + i }
println(sumWithInitialValue) // Output: 30

Best Practices

  • Use Immutable Collections: Prefer immutable collections (List, Set) over mutable ones (MutableList, MutableSet) unless mutability is necessary.

  • Chaining Functions: Kotlin's collection functions are composable, allowing you to chain them together for concise and readable code.

    val result = numbers.filter { it % 2 == 0 }.map { it * 2 }
    
  • Avoid Unnecessary Copies: Be mindful of the performance implications of creating new collections. Use functions like toMutableList or toMutableSet only when necessary.

  • Use Named Arguments for Clarity: When using higher-order functions, use named arguments to improve code readability.

    val filteredAndMapped = numbers.filter { it % 2 == 0 }.map { number -> number * 2 }
    

Conclusion

Kotlin's collection functions provide a powerful and expressive way to manipulate data. By understanding and utilizing these functions effectively, you can write more concise, readable, and efficient code. Whether you're working with simple lists or complex data structures, Kotlin's collections API offers the tools you need to handle your data with ease.

Additional Resources

  • Kotlin Standard Library Documentation
  • Effective Kotlin by JetBrains

By following this guide, you'll be well-equipped to leverage Kotlin's collection functions in your projects. Happy coding!


PreviousKotlin Standard LibraryNext Sequences in Kotlin

Recommended Gear

Kotlin Standard LibrarySequences in Kotlin