Kotlin Tutorials

Kotlin Set flatMapIndexedTo()
Syntax & Examples

Set.flatMapIndexedTo() extension function

The flatMapIndexedTo() extension function in Kotlin appends all elements yielded from the results of the transform function being invoked on each element and its index in the original set to the given destination collection.


Syntax of Set.flatMapIndexedTo()

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

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

This flatMapIndexedTo() extension function of Set appends all elements yielded from results of transform function being invoked on each element and its index in the original collection, to the given destination.

Parameters

ParameterOptional/RequiredDescription
destinationrequiredThe collection to which the resulting elements will be appended.
transformrequiredA function that takes an index and an element and returns an iterable of results.

Return Type

Set.flatMapIndexedTo() returns value of type C.



✐ Examples

1 Appending flattened lists with index to a destination list

Using flatMapIndexedTo() to append the results of flattening a set of lists into a single list, including the index in the transformation, to a destination list.

For example,

  1. Create a set of lists of integers.
  2. Create an empty list to hold the resulting elements.
  3. Define a transform function that returns the list itself, adjusted by the index.
  4. Use flatMapIndexedTo() to append the results to the destination list.
  5. Print the resulting list.

Kotlin Program

fun main() {
    val setOfLists: Set<List<Int>> = setOf(listOf(1, 2), listOf(3, 4), listOf(5))
    val destinationList = mutableListOf<Int>()
    setOfLists.flatMapIndexedTo(destinationList) { index, list -> list.map { it + index } }
    println(destinationList)
}

Output

[1, 2, 4, 5, 7]

2 Appending flattened strings into characters with index to a destination set

Using flatMapIndexedTo() to append the results of flattening a set of strings into a single list of characters, including the index in the transformation, to a destination set.

For example,

  1. Create a set of strings.
  2. Create an empty set to hold the resulting characters.
  3. Define a transform function that returns the characters of each string as an iterable, adjusted by the index.
  4. Use flatMapIndexedTo() to append the results to the destination set.
  5. Print the resulting set of characters.

Kotlin Program

fun main() {
    val setOfStrings: Set<String> = setOf("one", "two", "three")
    val destinationSet = mutableSetOf<Char>()
    setOfStrings.flatMapIndexedTo(destinationSet) { index, str -> str.map { (it + index).toChar() } }
    println(destinationSet)
}

Output

[o, n, e, u, x, p, v, k, u, e, g, i]

3 Appending flattened integers to their factors with index to a destination list

Using flatMapIndexedTo() to append the results of flattening a set of integers into a single list of their factors, including the index in the transformation, to a destination list.

For example,

  1. Create a set of integers.
  2. Create an empty list to hold the resulting factors.
  3. Define a transform function that returns the factors of each integer as an iterable, adjusted by the index.
  4. Use flatMapIndexedTo() to append the results to the destination list.
  5. Print the resulting list of factors.

Kotlin Program

fun main() {
    val setOfIntegers: Set<Int> = setOf(6, 8)
    val destinationList = mutableListOf<Int>()
    setOfIntegers.flatMapIndexedTo(destinationList) { index, number -> (1..number).filter { number % it == 0 }.map { it + index } }
    println(destinationList)
}

Output

[1, 3, 4, 7, 2, 3, 5, 9]

Summary

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