Kotlin Set mapIndexedNotNull()
Syntax & Examples


Set.mapIndexedNotNull() extension function

The mapIndexedNotNull() extension function in Kotlin returns a list containing only the non-null results of applying the given transform function to each element and its index in the original set.


Syntax of Set.mapIndexedNotNull()

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

fun <T, R : Any> Set<T>.mapIndexedNotNull(transform: (index: Int, T) -> R?): List<R>

This mapIndexedNotNull() extension function of Set returns a list containing only the non-null results of applying the given transform function to each element and its index in the original collection.

Parameters

ParameterOptional/RequiredDescription
transformrequiredA function that takes an index and an element, and returns the transformed result, which may be null.

Return Type

Set.mapIndexedNotNull() returns value of type List.



✐ Examples

1 Transforming a set of integers by adding their indices, filtering out nulls

Using mapIndexedNotNull() to transform a set of integers by adding their indices to each element, and filtering out null results.

For example,

  1. Create a set of integers.
  2. Use mapIndexedNotNull() with a transform function that adds the index to each element and filters out nulls.
  3. Print the resulting list.

Kotlin Program

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

Output

[1, 5, 9]

2 Transforming a set of strings by concatenating their indices, filtering out nulls

Using mapIndexedNotNull() to transform a set of strings by concatenating their indices to each string, and filtering out null results.

For example,

  1. Create a set of strings.
  2. Use mapIndexedNotNull() with a transform function that concatenates the index to each string and filters out nulls.
  3. Print the resulting list.

Kotlin Program

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

Output

[2: three]

3 Transforming a set of custom objects by including their indices, filtering out nulls

Using mapIndexedNotNull() to transform a set of custom objects by including their indices in the transformation, and filtering out null results.

For example,

  1. Create a data class.
  2. Create a set of custom objects.
  3. Use mapIndexedNotNull() with a transform function that includes the index in the transformation and filters out nulls.
  4. 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 indexedPeople = people.mapIndexedNotNull { index, person -> if (person.age > 30) "$index: ${person.name}, ${person.age}" else null }
    println(indexedPeople)
}

Output

[2: Charlie, 35]

Summary

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