Kotlin Tutorials

Kotlin Set flatMapIndexed()
Syntax & Examples

Set.flatMapIndexed() extension function

The flatMapIndexed() extension function in Kotlin returns a single list of all elements yielded from the results of the transform function being invoked on each element and its index in the original set.


Syntax of Set.flatMapIndexed()

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

fun <T, R> Set<T>.flatMapIndexed(transform: (index: Int, T) -> Iterable<R>): List<R>

This flatMapIndexed() extension function of Set returns a single list of all elements yielded from results of transform function being invoked on each element and its index in the original collection.

Parameters

ParameterOptional/RequiredDescription
transformrequiredA function that takes an index and an element and returns an iterable of results.

Return Type

Set.flatMapIndexed() returns value of type List.



✐ Examples

1 Flattening a set of lists with index

Using flatMapIndexed() to flatten a set of lists into a single list, including the index in the transformation.

For example,

  1. Create a set of lists of integers.
  2. Define a transform function that returns the list itself.
  3. Use flatMapIndexed() to flatten the set of lists into a single list, including the index in the transformation.
  4. Print the resulting list.

Kotlin Program

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

Output

[1, 2, 4, 5, 7]

2 Flattening a set of strings into characters with index

Using flatMapIndexed() to flatten a set of strings into a single list of characters, including the index in the transformation.

For example,

  1. Create a set of strings.
  2. Define a transform function that returns the characters of each string as an iterable, adjusted by the index.
  3. Use flatMapIndexed() to flatten the set of strings into a single list of characters, including the index in the transformation.
  4. Print the resulting list of characters.

Kotlin Program

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

Output

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

3 Flattening a set of integers to their factors with index

Using flatMapIndexed() to flatten a set of integers into a single list of their factors, including the index in the transformation.

For example,

  1. Create a set of integers.
  2. Define a transform function that returns the factors of each integer as an iterable, adjusted by the index.
  3. Use flatMapIndexed() to flatten the set of integers into a single list of their factors, including the index in the transformation.
  4. Print the resulting list of factors.

Kotlin Program

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

Output

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

Summary

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