Kotlin Set onEachIndexed()
Syntax & Examples


Set.onEachIndexed() extension function

The onEachIndexed() extension function in Kotlin performs the given action on each element, providing the sequential index with the element, and returns the collection itself afterwards.


Syntax of Set.onEachIndexed()

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

fun <T, C : Iterable<T>> C.onEachIndexed(action: (index: Int, T) -> Unit): C

This onEachIndexed() extension function of Set performs the given action on each element, providing sequential index with the element, and returns the collection itself afterwards.

Parameters

ParameterOptional/RequiredDescription
actionrequiredThe action to be performed on each element, providing the sequential index.

Return Type

Set.onEachIndexed() returns value of type C.



✐ Examples

1 Performing an action on each element in a set of integers with index

Using onEachIndexed() to print each element and its index in a set of integers.

For example,

  1. Create a set of integers.
  2. Use onEachIndexed() with an action that prints each element and its index.
  3. Print the original set to show it is unchanged.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5)
    numbers.onEachIndexed { index, value -> println("Index: $index, Value: $value") }
    println(numbers)
}

Output

Index: 0, Value: 1
Index: 1, Value: 2
Index: 2, Value: 3
Index: 3, Value: 4
Index: 4, Value: 5
[1, 2, 3, 4, 5]

2 Modifying and performing an action on each element in a set of strings with index

Using onEachIndexed() to print each element in uppercase and its index.

For example,

  1. Create a set of strings.
  2. Use onEachIndexed() with an action that converts each element to uppercase and prints it along with its index.
  3. Print the original set to show it is unchanged.

Kotlin Program

fun main() {
    val strings = setOf("one", "two", "three")
    strings.onEachIndexed { index, value -> println("Index: $index, Value: ${value.uppercase()}") }
    println(strings)
}

Output

Index: 0, Value: ONE
Index: 1, Value: TWO
Index: 2, Value: THREE
[one, two, three]

3 Performing an action on each custom object in a set with index

Using onEachIndexed() to print the name of each custom object in a set along with its index.

For example,

  1. Create a data class.
  2. Create a set of custom objects.
  3. Use onEachIndexed() with an action that prints the name of each object along with its index.
  4. Print the original set to show it is unchanged.

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))
    people.onEachIndexed { index, person -> println("Index: $index, Name: ${person.name}") }
    println(people)
}

Output

Index: 0, Name: Alice
Index: 1, Name: Bob
Index: 2, Name: Charlie
[Person(name=Alice, age=30), Person(name=Bob, age=25), Person(name=Charlie, age=35)]

Summary

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