Kotlin Set reduceOrNull()
Syntax & Examples


Set.reduceOrNull() extension function

The reduceOrNull() extension function in Kotlin accumulates a value starting with the first element and applying an operation from left to right to the current accumulator value and each element. If the collection is empty, it returns null.


Syntax of Set.reduceOrNull()

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

fun <S, T : S> Set<T>.reduceOrNull(operation: (acc: S, T) -> S): S?

This reduceOrNull() extension function of Set accumulates value starting with the first element and applying operation from left to right to current accumulator value and each element.

Parameters

ParameterOptional/RequiredDescription
operationrequiredA function that takes the current accumulator value and an element, and returns the new accumulator value.

Return Type

Set.reduceOrNull() returns value of type S?.



✐ Examples

1 Summing elements in a set of integers or returning null if empty

Using reduceOrNull() to sum the elements in a set of integers, or return null if the set is empty.

For example,

  1. Create a set of integers.
  2. Use reduceOrNull() with an operation that adds each element to the accumulator.
  3. Print the resulting sum or null.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5)
    val sum = numbers.reduceOrNull { acc, num -> acc + num }
    println(sum)
}

Output

15

2 Concatenating strings in a set or returning null if empty

Using reduceOrNull() to concatenate the strings in a set, or return null if the set is empty.

For example,

  1. Create a set of strings.
  2. Use reduceOrNull() with an operation that concatenates each string to the accumulator.
  3. Print the resulting concatenated string or null.

Kotlin Program

fun main() {
    val strings = setOf("Kotlin", "is", "fun")
    val result = strings.reduceOrNull { acc, str -> "$acc $str" }
    println(result)
}

Output

Kotlin is fun

3 Calculating the product of elements in a set of integers or returning null if empty

Using reduceOrNull() to calculate the product of the elements in a set of integers, or return null if the set is empty.

For example,

  1. Create a set of integers.
  2. Use reduceOrNull() with an operation that multiplies each element to the accumulator.
  3. Print the resulting product or null.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4)
    val product = numbers.reduceOrNull { acc, num -> acc * num }
    println(product)
}

Output

24

4 Handling an empty set with reduceOrNull()

Using reduceOrNull() to handle an empty set and return null.

For example,

  1. Create an empty set of integers.
  2. Use reduceOrNull() with an operation that adds each element to the accumulator.
  3. Print the resulting value or null.

Kotlin Program

fun main() {
    val emptySet = emptySet<Int>()
    val result = emptySet.reduceOrNull { acc, num -> acc + num }
    println(result)
}

Output

null

Summary

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