Skip to content
Related Articles

Related Articles

Improve Article

Kotlin – Collection Operations Overview

  • Last Updated : 28 Aug, 2019

The Kotlin standard library provides a wide range of functions for performing operations on collections. It includes simple operations like getting or adding elements and also includes more complex ones like searching, sorting, filtering etc.

Member and Extension functions –

Collection operations are declared in two ways:

  • Member functions
  • Extension functions

Member functions –

It define the operations that are requisite for a collection type. For example, Collection contains the function isEmpty() for checking its emptiness; List contains get() for index access to elements, and so on.

We must implement member function while creating own implementation of collection interface. In order to create the new implementation easier, we can use the skeletal implementations of collection interfaces from the Kotlin standard library like AbstractList, AbstractSet, AbstractMap, and also their mutable counterparts.

Extension function –

All the operations except member function are declared as extension functions. Some of these are filtering, transformation, ordering, and other collection processing functions.

Common operations –

Common operations are available for both mutable collections(read and write) and read-only. Common operations fall into these categories:

  • Transformations
  • Filtering
  • plus and minus operators
  • Grouping
  • Retrieving collection parts
  • Retrieving single elements
  • Ordering
  • Aggregate operations

All the operations discussed above return their results without affecting the original content of the collection. For example, when we apply filtering operation then it produces a new collection that contains all the elements matching the filtering predicate. Results of these operations should be either stored in variables or could be passed to other functions.

Kotlin program using filter operation –

fun main() {
    val str = mutableListOf("Geeks", "for", "Geeks", "A", "Computer", "Portal")
    // original collection remains same 
    str.filter { it.length > 4 }
    println("Original collection elements still unchanged $str")
    // result is stored in newStr
    val newStr = str.filter { it.length > 4 }
    println("New Collection obtains after filtering $newStr")


Original collection elements still unchanged [Geeks, for, Geeks, A, Computer, Portal]
New Collection obtains after filtering [Geeks, Geeks, Computer, Portal]

For limited collection operations, we can specify the destination object and it is optional. Destination should be mutable collection to which the function add its resulting items instead of returning them in a new object. To perform these operations with destinations, we can separate functions with the To postfix notation in their names, for example, use filterTo() instead of filter().

Kotlin program of using destination object –

fun main() {
    val str = listOf("Geeks", "for", "Geeks", "A", "Computer", "Portal")
    //destination object
    val filterResults = mutableListOf<String>()
    str.filterTo(filterResults) { it.length > 5 }
    str.filterIndexedTo(filterResults) { index, _ -> index == 0 }
    // filterResults contains results of both operations
    println("Combined Result of both operations $filterResults")


Combined Result of both operations [Computer, Portal, Geeks]

As we discussed above we can pass the collection operation result to another function and these functions return the destination collection back, so we can create it right in the corresponding argument of the function call:

Kotlin program of storing result in Hashset –

fun main() {
    val str = listOf("Geeks", "for", "Geeks", "A", "Computer", "Portal")
    // filter strings right into a new hash set,
    // and eliminating duplicates in the result
    val result = str.mapTo(HashSet()) { it.length }
    // print hashset
    println("Only Distinct item length return by hashset $result")


Only Distinct item length return by hashset [1, 3, 5, 6, 8]

Write operations –

There are also write operations that can change the collection state in case of mutable collections. These operations include adding, removing, and updating elements in the collection. Write operations are listed in the Write operations and corresponding sections of List specific operations and Map specific operations.

For specific operations, there are pairs of functions for performing the same operation on the collections: first one can applies the operation in-place means perform operation in the original collection and the other returns the result as a new separate collection.

For example: While applying sort() operation, it sorts a mutable collection in-place, so it’s state changes but in case of sorted() operation, it creates a new collection that contains the same elements in the sorted order.

Kotlin program of using sort() and sorted() operation –

fun main() {
    val units = mutableListOf("Ten","Hundred","Thousand","Lakh","Crore")
    // sorted() creates new collection and sort
    val sortedNumbers = units.sorted()
    println(units == sortedNumbers) // false
    // sort() sort in same collection
    println(units == sortedNumbers) // true



My Personal Notes arrow_drop_up
Recommended Articles
Page :