Kotlin Tutorials

Kotlin Set maxOfOrNull()
Syntax & Examples

Set.maxOfOrNull() extension function

The maxOfOrNull() extension function in Kotlin returns the largest value among all values produced by the selector function applied to each element in the collection, or null if there are no elements.


Syntax of Set.maxOfOrNull()

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

fun <T> Set<T>.maxOfOrNull(selector: (T) -> Double): Double?

This maxOfOrNull() extension function of Set returns the largest value among all values produced by selector function applied to each element in the collection or null if there are no elements.

Parameters

ParameterOptional/RequiredDescription
selectorrequiredA function that takes an element and returns a Double value to be compared.

Return Type

Set.maxOfOrNull() returns value of type Double?.



✐ Examples

1 Finding the maximum value among integers transformed to double or null if empty

Using maxOfOrNull() to find the maximum value among integers in a set transformed to double, or null if the set is empty.

For example,

  1. Create a set of integers.
  2. Use maxOfOrNull() with a selector function that transforms each integer to double.
  3. Print the resulting maximum value or null.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3, 4, 5)
    val maxValue = numbers.maxOfOrNull { it.toDouble() }
    println(maxValue)
}

Output

5.0

2 Finding the maximum length of strings or null if empty

Using maxOfOrNull() to find the maximum length among strings in a set, or null if the set is empty.

For example,

  1. Create a set of strings.
  2. Use maxOfOrNull() with a selector function that returns the length of each string as a double.
  3. Print the resulting maximum length or null.

Kotlin Program

fun main() {
    val strings = setOf("one", "two", "three")
    val maxLength = strings.maxOfOrNull { it.length.toDouble() }
    println(maxLength)
}

Output

5.0

3 Finding the maximum age among custom objects or null if empty

Using maxOfOrNull() to find the maximum age among custom objects in a set, or null if the set is empty.

For example,

  1. Create a data class.
  2. Create a set of custom objects.
  3. Use maxOfOrNull() with a selector function that returns the age of each object as a double.
  4. Print the resulting maximum age or null.

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 maxAge = people.maxOfOrNull { it.age.toDouble() }
    println(maxAge)
}

Output

35.0

4 Handling an empty set

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

For example,

  1. Create an empty set of integers.
  2. Use maxOfOrNull() with a selector function that transforms each integer to double.
  3. Print the resulting maximum value or null.

Kotlin Program

fun main() {
    val emptySet = emptySet<Int>()
    val maxValue = emptySet.maxOfOrNull { it.toDouble() }
    println(maxValue)
}

Output

null

Summary

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