Map Operator : Kotlin

Suneet Agrawal
4 min readMay 7, 2023

The collection is something which is used by almost everyone. It makes our life easy. List, Set and Map are the best examples of them.

To iterate, filter or modify the existing collection object, Kotlin provides us with a few in builds transform operators. Map operator is one of them.
Lets try to understand the map operator in detail.`This post was originally posted at https://agrawalsuneet.github.io/blogs/math-round-vs-math-floor-vs-math-ceil-kotlin/ and reposted on Medium on 13th April 2022.

This post was originally posted at https://agrawalsuneet.github.io/blogs/map-operator-kotlin/ and later reposted on Medium.

What is map operator?

Map operator is used to create a new collection object by iterating over the existing collection object and applying some transformation to it. The transformation that needs to be applied is passed as lambda to the function.

Since the map operator is an extension of the Iterable Interface, it can be used with all three collections ie List, Set and Map.

/**
* Returns a list containing the results of applying the given [transform] function
* to each element in the original collection.
*
* @sample samples.collections.Collections.Transformations.map
*/
public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
}

Map also has other few functions as below,

  • mapNotNull
    used for iteration and apply a transform to it but only to the non-null values. This will skip all null values in the list.
  • mapIndexed
    used for indexed iteration and apply a transform to it.
  • mapIndexedNotNull
    used for indexed iteration and apply a transform to it but only to the non-null values. This will skip all null values in the list.
/**
* Returns a list containing only the non-null results of applying the given [transform] function
* to each element in the original collection.
*
* @sample samples.collections.Collections.Transformations.mapNotNull
*/
public inline fun <T, R : Any> Iterable<T>.mapNotNull(transform: (T) -> R?): List<R> {
return mapNotNullTo(ArrayList<R>(), transform)
}

/**
* Returns a list containing the results of applying the given [transform] function
* to each element and its index in the original collection.
* @param [transform] function that takes the index of an element and the element itself
* and returns the result of the transform applied to the element.
*/
public inline fun <T, R> Iterable<T>.mapIndexed(transform: (index: Int, T) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
}

/**
* Returns a list containing only the non-null results of applying the given [transform] function
* to each element and its index in the original collection.
* @param [transform] function that takes the index of an element and the element itself
* and returns the result of the transform applied to the element.
*/
public inline fun <T, R : Any> Iterable<T>.mapIndexedNotNull(transform: (index: Int, T) -> R?): List<R> {
return mapIndexedNotNullTo(ArrayList<R>(), transform)
}

Map with List

map operator can be used over a list to transform its elements and return the resultant list. It can be used with a named parameter or using it.

val list = listOf(1, 2, 3, 4, 5)

val mapList = list.map{item -> item * 10}
println(mapList)

val mapList2 = list.map{ it * 10 }
println(mapList2)

val mapNotNullList = list.mapNotNull{ if (it % 2 == 0) null else it }
println(mapNotNullList)

map can also be used to transform a list of one data type to a list of another data type. In the below example, List of Int is converted to List of String.

val list = listOf(1 , 2, 3, 4, 5)

val stringList = list.map{ it.toString()}

println(stringList::class.simpleName)
//this will print: ArrayList

println(stringList[0]::class.simpleName)
//this will print: String

In the case of mapIndexed and mapIndexedNotNull, since we have two variables passed in the lambda function, we can not skip the named parameters.

val list = listOf(1, 2, 3, 4, 5)

val mapIndexedList = list.mapIndexed{index, item -> index * item }
println(mapIndexedList)

val mapIndexedNotNullList = list.mapIndexedNotNull{
index, item -> if (it % 2 == 0) null
else (item * index)
}
println(mapIndexedNotNullList)

Map with Set

Similar to List, map operator can be used over a set to transform its elements and return the resultant set. It can be used with a named parameter or using it.

Please continue reading at https://agrawalsuneet.github.io/blogs/map-operator-kotlin/

That’s all for now. You can read my other interesting blogs here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--