Kotlin Set minOfWithOrNull()
Syntax & Examples


Set.minOfWithOrNull() extension function

The minOfWithOrNull() extension function in Kotlin returns the smallest value according to the provided comparator 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.minOfWithOrNull()

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

fun <T, R> Set<T>.minOfWithOrNull(comparator: Comparator<in R>, selector: (T) -> R): R?

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

Parameters

ParameterOptional/RequiredDescription
comparatorrequiredThe comparator used to compare the values.
selectorrequiredA function that takes an element and returns a value to be compared.

Return Type

Set.minOfWithOrNull() returns value of type R?.



✐ Examples

1 Finding the minimum value among integers with a custom comparator or null if empty

Using minOfWithOrNull() to find the minimum value among integers in a set with a custom comparator, or null if the set is empty.

For example,

  1. Create a set of integers.
  2. Use minOfWithOrNull() with a custom comparator and a selector function that transforms each integer.
  3. Print the resulting minimum value or null.

Kotlin Program

fun main() {
    val numbers = setOf(3, 1, 4, 1, 5)
    val minValue = numbers.minOfWithOrNull(compareBy { it }, { it.toDouble() })
    println(minValue)
}

Output

1.0

2 Finding the minimum length of strings with a custom comparator or null if empty

Using minOfWithOrNull() to find the minimum length among strings in a set with a custom comparator, or null if the set is empty.

For example,

  1. Create a set of strings.
  2. Use minOfWithOrNull() with a custom comparator and a selector function that returns the length of each string.
  3. Print the resulting minimum length or null.

Kotlin Program

fun main() {
    val strings = setOf("apple", "pear", "banana")
    val minLength = strings.minOfWithOrNull(compareBy { it }, { it.length.toDouble() })
    println(minLength)
}

Output

4.0

3 Finding the minimum age among custom objects with a custom comparator or null if empty

Using minOfWithOrNull() to find the minimum age among custom objects in a set with a custom comparator, or null if the set is empty.

For example,

  1. Create a data class.
  2. Create a set of custom objects.
  3. Use minOfWithOrNull() with a custom comparator and a selector function that returns the age of each object.
  4. Print the resulting minimum 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 minAge = people.minOfWithOrNull(compareBy { it }, { it.age.toDouble() })
    println(minAge)
}

Output

25.0

4 Handling an empty set with a custom comparator

Using minOfWithOrNull() to handle an empty set and return null with a custom comparator.

For example,

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

Kotlin Program

fun main() {
    val emptySet = emptySet<Int>()
    val minValue = emptySet.minOfWithOrNull(compareBy { it }, { it.toDouble() })
    println(minValue)
}

Output

null

Summary

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