Kotlin Tutorials

Kotlin Set mapTo()
Syntax & Examples

Set.mapTo() extension function

The mapTo() extension function in Kotlin applies the given transform function to each element of the original set, and appends the results to the given destination.


Syntax of Set.mapTo()

The syntax of Set.mapTo() extension function is:

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

This mapTo() extension function of Set applies the given transform function to each element of the original collection and appends the results to the given destination.

Parameters

ParameterOptional/RequiredDescription
destinationrequiredThe collection to which the results will be appended.
transformrequiredA function that takes an element and returns the transformed result.

Return Type

Set.mapTo() returns value of type C.



✐ Examples

1 Transforming a set of integers by doubling each element and appending to a list

Using mapTo() to transform a set of integers by doubling each element, and appending them to a list.

For example,

  1. Create a set of integers.
  2. Create an empty mutable list to serve as the destination.
  3. Use mapTo() with a transform function that doubles each element.
  4. Print the resulting list.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5)
    val destination = mutableListOf<Int>()
    numbers.mapTo(destination) { it * 2 }
    println(destination)
}

Output

[2, 4, 6, 8, 10]

2 Transforming a set of strings by getting their lengths and appending to a list

Using mapTo() to transform a set of strings by getting the length of each string, and appending them to a list.

For example,

  1. Create a set of strings.
  2. Create an empty mutable list to serve as the destination.
  3. Use mapTo() with a transform function that returns the length of each string.
  4. Print the resulting list.

Kotlin Program

fun main() {
    val strings = setOf("one", "two", "three")
    val destination = mutableListOf<Int>()
    strings.mapTo(destination) { it.length }
    println(destination)
}

Output

[3, 3, 5]

3 Transforming a set of custom objects by extracting a specific property and appending to a list

Using mapTo() to transform a set of custom objects by extracting a specific property, and appending them to a list.

For example,

  1. Create a data class.
  2. Create a set of custom objects.
  3. Create an empty mutable list to serve as the destination.
  4. Use mapTo() with a transform function that extracts a specific property from each object.
  5. Print the resulting list.

Kotlin Program

data class Person(val name: String, val age: Int)

fun main() {
    val people = setOf(Person("Alice", 30), Person("Bob", 25), Person("Charlie", 35))
    val destination = mutableListOf<String>()
    people.mapTo(destination) { it.name }
    println(destination)
}

Output

[Alice, Bob, Charlie]

Summary

In this Kotlin tutorial, we learned about mapTo() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.