Kotlin Tutorials

Kotlin Set mapIndexedTo()
Syntax & Examples

Set.mapIndexedTo() extension function

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


Syntax of Set.mapIndexedTo()

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

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

This mapIndexedTo() extension function of Set applies the given transform function to each element and its index in 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 index and an element, and returns the transformed result.

Return Type

Set.mapIndexedTo() returns value of type C.



✐ Examples

1 Transforming a set of integers by adding their indices and appending to a list

Using mapIndexedTo() to transform a set of integers by adding their indices to 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 mapIndexedTo() with a transform function that adds the index to each element.
  4. Print the resulting list.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5)
    val destination = mutableListOf<Int>()
    numbers.mapIndexedTo(destination) { index, value -> index + value }
    println(destination)
}

Output

[1, 3, 5, 7, 9]

2 Transforming a set of strings by concatenating their indices and appending to a list

Using mapIndexedTo() to transform a set of strings by concatenating their indices to 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 mapIndexedTo() with a transform function that concatenates the index to each string.
  4. Print the resulting list.

Kotlin Program

fun main() {
    val strings = setOf("one", "two", "three")
    val destination = mutableListOf<String>()
    strings.mapIndexedTo(destination) { index, value -> "$index: $value" }
    println(destination)
}

Output

[0: one, 1: two, 2: three]

3 Transforming a set of custom objects by including their indices and appending to a list

Using mapIndexedTo() to transform a set of custom objects by including their indices in the transformation, 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 mapIndexedTo() with a transform function that includes the index in the transformation.
  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.mapIndexedTo(destination) { index, person -> "$index: ${person.name}, ${person.age}" }
    println(destination)
}

Output

[0: Alice, 30, 1: Bob, 25, 2: Charlie, 35]

Summary

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