![]() |
VOOZH | about |
The Kotlin Standard Library provides a rich set of extension functions for transforming collections. These transformations help us to build new collections or values derived from existing collections based on specific transformation logic. Kotlin supports several types of collection transformations, such as:
In this article, we will explore each of these transformations with simple explanations and examples.
Mapping transforms the elements of a collection by applying a function to each element and returning a new collection containing the results. The core function for mapping is map().
Example:
Output:
The new elements are: [2, 4, 6]
The modified elements are: [2, 2, 6]
After transformation if we obtain null values on certain elements, we can easily filter out nulls from the collection only by calling the mapNotNull() function instead of map(), or mapIndexedNotNull() instead of mapIndexed().
Example: Removing Null Values with mapNotNull()
Output:
Numbers without null value: [2, 4, 6]Zipping transformation help in building pairs by picking elements from both collections from the same indexing value and it can be done with the help of zip() extension function. It can be called on a collection or an array by passing another collection (array) as an argument and it returns the List of Pair objects.
The first elements of pairs from the receiving collection and the second from the collection passed as an argument. If the collections sizes do not match then the resultant collection of the zip() is equivalent to the smaller size collection and the last elements of the larger collection are excluded from the results. It can also be called in the infix form a zip b.
Example:
Output:
[(One, 1), (Two, 2), (Three, 3)]If we have a List of Pairs then we can do the reverse transformation with the help unzipping extension function which builds two lists from these pairs:
Example: Unzipping collections
Output:
Unzipped: [Apple, Google, Amazon, Facebook] and [1, 2, 3, 4]Association transformations help in building maps from the collection elements and certain values associated with them. Here, the basic association function is associateWith() which creates a Map in which original collection elements are the keys and associated values are obtained by the given transformation function from the original elements.
Note: If we get two elements that are equal then only the last one remains on the map.
Example:
Output:
{Kohli=5, Root=4, Smith=5, Williamson=10}Flatten transformation helps in converting all the nested collections into single collection. If we operate nested collections, we find the standard library functions that provide flat access to nested collection elements useful.
The basic function is flatten() and we can call it on a collection of collections, for example, a List of Sets then it returns a single List of all the elements of the nested collections.
Example:
Output:
All openers: [Warner, Finch, Roy, Bairstow, Rohit, Dhawan, Guptill, Henry] String representation means we can retrieve the collection content in a readable format. There are two functions that transform the collections to strings:
The function joinToString() builds a single String from the collection elements based on the provided arguments. And function joinTo() also builds a single string but appends the result to the given Appendable object.
When the above functions called with the default arguments, both return the result similar to calling toString() on the collection: a String of elements' string representations separated by commas with spaces.
Example:
Output:
[Red, Green, Blue, Orange, Yellow]
Red, Green, Blue, Orange, Yellow
Colors are: Red, Green, Blue, Orange, Yellow