Kotlin mapTo Function

The mapTo function in Kotlin is used to transform elements of a collection by applying a given function to each element and storing the results in a specified destination collection. This function belongs to the Kotlin standard library and provides a flexible way to apply transformations and collect the results in a custom collection.

Table of Contents

  1. Introduction
  2. mapTo Function Syntax
  3. Understanding mapTo
  4. Examples
    • Basic Usage
    • Mapping to a Mutable List
    • Mapping to a Mutable Set
  5. Real-World Use Case
  6. Conclusion

Introduction

The mapTo function allows you to transform elements of a collection (such as a list or set) by applying a given function to each element and storing the resulting elements in a specified destination collection. This is useful for scenarios where you need to collect the transformed results in a custom collection.

mapTo Function Syntax

The syntax for the mapTo function is as follows:

fun <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo(destination: C, transform: (T) -> R): C

Parameters:

  • destination: The collection where the transformed elements will be stored.
  • transform: A lambda function that takes an element of the original collection as input and returns a transformed value.

Returns:

  • The destination collection containing the transformed elements.

Understanding mapTo

The mapTo function iterates over each element in the original collection, applies the given transformation function to it, and adds the resulting elements to the specified destination collection. This allows you to perform transformations and collect the results in a custom collection in one step.

Examples

Basic Usage

To demonstrate the basic usage of mapTo, we will transform a list of integers by multiplying each element by 2 and storing the results in a mutable list.

Example

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val doubledNumbers = mutableListOf<Int>()
    numbers.mapTo(doubledNumbers) { it * 2 }
    println("Doubled numbers: $doubledNumbers")
}

Output:

Doubled numbers: [2, 4, 6, 8, 10]

Mapping to a Mutable List

This example shows how to use mapTo to transform elements and store the results in a mutable list.

Example

fun main() {
    val fruits = listOf("apple", "banana", "cherry")
    val uppercasedFruits = mutableListOf<String>()
    fruits.mapTo(uppercasedFruits) { it.uppercase() }
    println("Uppercased fruits: $uppercasedFruits")
}

Output:

Uppercased fruits: [APPLE, BANANA, CHERRY]

Mapping to a Mutable Set

This example demonstrates how to use mapTo to transform elements and store the results in a mutable set.

Example

fun main() {
    val numbers = listOf(1, 2, 2, 3, 4, 4, 5)
    val uniqueDoubledNumbers = mutableSetOf<Int>()
    numbers.mapTo(uniqueDoubledNumbers) { it * 2 }
    println("Unique doubled numbers: $uniqueDoubledNumbers")
}

Output:

Unique doubled numbers: [2, 4, 6, 8, 10]

Real-World Use Case

Transforming Orders and Collecting in a Custom Collection

In real-world applications, the mapTo function can be used to transform orders based on specific criteria and collect the results in a custom collection, such as a list or set.

Example

data class Order(val id: Int, val amount: Double)
data class OrderSummary(val id: Int, val amount: Double, val tax: Double)

fun main() {
    val orders = listOf(
        Order(1, 100.0),
        Order(2, 150.0),
        Order(3, 200.0)
    )
    val orderSummaries = mutableListOf<OrderSummary>()
    orders.mapTo(orderSummaries) { OrderSummary(it.id, it.amount, it.amount * 0.1) }
    println("Order summaries: $orderSummaries")
}

Output:

Order summaries: [OrderSummary(id=1, amount=100.0, tax=10.0), OrderSummary(id=2, amount=150.0, tax=15.0), OrderSummary(id=3, amount=200.0, tax=20.0)]

Conclusion

The mapTo function in Kotlin is a powerful and flexible way to transform elements of a collection by applying a given function to each element and storing the results in a specified destination collection. It allows you to perform transformations and collect the results in custom collections, making it useful for various applications, including data processing and transformation. 

By understanding and using the mapTo function, you can effectively manage and process collections in your Kotlin applications.

Comments