Kotlin Set filterIndexedTo()
Syntax & Examples


Set.filterIndexedTo() extension function

The filterIndexedTo() extension function in Kotlin filters elements in a set based on a given predicate that takes both the index and the element as arguments, appending the matching elements to the specified destination collection.


Syntax of Set.filterIndexedTo()

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

fun <T, C : MutableCollection<in T>> Set<T>.filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean): C

This filterIndexedTo() extension function of Set appends all elements matching the given predicate to the given destination.

Parameters

ParameterOptional/RequiredDescription
destinationrequiredThe collection to which the filtered elements will be appended.
predicaterequiredA function that takes the index of an element and the element itself, returning true if the element should be included in the destination collection.

Return Type

Set.filterIndexedTo() returns value of type C.



✐ Examples

1 Filtering even-indexed elements

Using filterIndexedTo() to filter elements at even indices in a set and append them to a new list.

For example,

  1. Create a set of integers from 1 to 10.
  2. Define a predicate function that returns true for even indices.
  3. Use filterIndexedTo() to filter elements at even indices and append them to a new list.
  4. Print the new list.

Kotlin Program

fun main(args: Array<String>) {
    val numbers = setOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    val evenIndexed = mutableListOf<Int>()
    numbers.filterIndexedTo(evenIndexed) { index, _ -> index % 2 == 0 }
    println(evenIndexed)
}

Output

[1, 3, 5, 7, 9]

2 Filtering elements greater than their indices

Using filterIndexedTo() to filter elements in a set that are greater than their indices and append them to a new list.

For example,

  1. Create a set of integers from 1 to 10.
  2. Define a predicate function that returns true if the element is greater than its index.
  3. Use filterIndexedTo() to filter such elements and append them to a new list.
  4. Print the new list.

Kotlin Program

fun main(args: Array<String>) {
    val numbers = setOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    val greaterThanIndex = mutableListOf<Int>()
    numbers.filterIndexedTo(greaterThanIndex) { index, value -> value > index }
    println(greaterThanIndex)
}

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

3 Filtering elements at odd indices to a set

Using filterIndexedTo() to filter elements at odd indices in a set and append them to a new set.

For example,

  1. Create a set of integers from 1 to 10.
  2. Define a predicate function that returns true for odd indices.
  3. Use filterIndexedTo() to filter elements at odd indices and append them to a new set.
  4. Print the new set.

Kotlin Program

fun main(args: Array<String>) {
    val numbers = setOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    val oddIndexedSet = mutableSetOf<Int>()
    numbers.filterIndexedTo(oddIndexedSet) { index, _ -> index % 2 != 0 }
    println(oddIndexedSet)
}

Output

[2, 4, 6, 8, 10]

Summary

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