Kotlin Tutorials

Kotlin Set mapIndexedNotNullTo()
Syntax & Examples

Set.mapIndexedNotNullTo() extension function

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


Syntax of Set.mapIndexedNotNullTo()

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

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

This mapIndexedNotNullTo() extension function of Set applies the given transform function to each element and its index in the original collection and appends only the non-null results to the given destination.

Parameters

ParameterOptional/RequiredDescription
destinationrequiredThe collection to which the non-null results will be appended.
transformrequiredA function that takes an index and an element, and returns the transformed result, which may be null.

Return Type

Set.mapIndexedNotNullTo() returns value of type C.



✐ Examples

1 Transforming a set of integers by adding their indices, filtering out nulls, and appending to a list

Using mapIndexedNotNullTo() to transform a set of integers by adding their indices to each element, filtering out null results, 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 mapIndexedNotNullTo() with a transform function that adds the index to each element and filters out nulls.
  4. Print the resulting list.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5)
    val destination = mutableListOf<Int>()
    numbers.mapIndexedNotNullTo(destination) { index, value -> if (index % 2 == 0) index + value else null }
    println(destination)
}

Output

[1, 5, 9]

2 Transforming a set of strings by concatenating their indices, filtering out nulls, and appending to a list

Using mapIndexedNotNullTo() to transform a set of strings by concatenating their indices to each string, filtering out null results, 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 mapIndexedNotNullTo() with a transform function that concatenates the index to each string and filters out nulls.
  4. Print the resulting list.

Kotlin Program

fun main() {
    val strings = setOf("one", "two", "three")
    val destination = mutableListOf<String>()
    strings.mapIndexedNotNullTo(destination) { index, value -> if (value.length > 3) "$index: $value" else null }
    println(destination)
}

Output

[2: three]

3 Transforming a set of custom objects by including their indices, filtering out nulls, and appending to a list

Using mapIndexedNotNullTo() to transform a set of custom objects by including their indices in the transformation, filtering out null results, 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 mapIndexedNotNullTo() with a transform function that includes the index in the transformation and filters out nulls.
  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.mapIndexedNotNullTo(destination) { index, person -> if (person.age > 30) "$index: ${person.name}, ${person.age}" else null }
    println(destination)
}

Output

[2: Charlie, 35]

Summary

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