Kotlin – Collection Operations Overview
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:
- plus and minus operators
- Retrieving collection parts
- Retrieving single elements
- 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 –
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 –
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 –
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 –