Sling Academy
Home/Kotlin/Transforming Lists with `map` and `flatMap` in Kotlin

Transforming Lists with `map` and `flatMap` in Kotlin

Last updated: December 05, 2024

Kotlin, a modern programming language, provides powerful transformations on collections. Among its many features, map and flatMap methods stand out as essential tools for transforming lists effectively and expressively. Understanding these functions can help you streamline your code and improve its readability.

Understanding map in Kotlin

The map function in Kotlin is used to transform each element of a collection by applying a given function to it, resulting in a new list. The transformation logic is encapsulated as a lambda expression, and the return value forms the new elements of the resulting list. Consider the following example to grasp its fundamentals:


fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val squares = numbers.map { it * it }
    println(squares) // Output: [1, 4, 9, 16, 25]
}

In the above code, the map function receives a lambda expression { it * it } that squares each element of the list, generating a new list of squared numbers.

Diving Deeper into flatMap

While map transforms individual elements, flatMap serves a dual purpose by both transforming each element and flattening multiple lists into a single list. This is particularly useful when you want to apply a transformation that results in lists, and then combine all those lists into a single collection. Consider this example:


fun main() {
    val nestedList = listOf(listOf(1, 2, 3), listOf(4, 5, 6), listOf(7, 8, 9))
    val flatList = nestedList.flatMap { it }
    println(flatList) // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
}

Here, flatMap is used to flatten a list of lists into a single list, effectively combining each sublist into a single sequence.

Taking it a step further, flatMap can be used to both transform and flatten collections:


fun main() {
    val words = listOf("hello", "world")
    val characters = words.flatMap { it.toList() }
    println(characters) // Output: [h, e, l, l, o, w, o, r, l, d]
}

In this example, each word in the list is transformed into a list of characters and the resulting lists are flattened into a single list of characters. This highlights flatMap's utility in performing complex transformation processes succinctly.

Choosing Between map and flatMap

The decision to use map or flatMap depends on the desired result:

  • Choose map when you need to transform each element into another form or structure.
  • Select flatMap when your transformation entails producing collections or sequences which need to be merged into a single list.

For instance, if you need to convert a list of input strings into a list of their uppercase variants, map is appropriate:


fun main() {
    val fruits = listOf("apple", "banana", "cherry")
    val upperCasedFruits = fruits.map { it.uppercase() }
    println(upperCasedFruits) // Output: [APPLE, BANANA, CHERRY]
}

However, if you wanted to convert a list of string phrases into individual words, followed by compiling all words into one list, flatMap would serve better:


fun main() {
    val phrases = listOf("hello world", "kotlin flatMap")
    val words = phrases.flatMap { it.split(" ") }
    println(words) // Output: [hello, world, kotlin, flatMap]
}

By effectively leveraging map and flatMap, you can write concise and expressive code that distinctly encapsulates transformation logic, making your code more comprehensible and cleaner.

Next Article: Grouping Lists by a Property in Kotlin

Previous Article: Partitioning Lists into Groups in Kotlin

Series: Kotlin Collections

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin