Kotlin Set minOfOrNull()
Syntax & Examples
Set.minOfOrNull() extension function
The minOfOrNull() extension function in Kotlin returns the smallest 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.minOfOrNull()
The syntax of Set.minOfOrNull() extension function is:
fun <T> Set<T>.minOfOrNull(selector: (T) -> Double): Double?
This minOfOrNull() extension function of Set returns the smallest value among all values produced by selector function applied to each element in the collection or null if there are no elements.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
selector | required | A function that takes an element and returns a Double value to be compared. |
Return Type
Set.minOfOrNull() returns value of type Double?
.
✐ Examples
1 Finding the minimum value among integers transformed to double or null if empty
Using minOfOrNull() to find the minimum value among integers in a set transformed to double, or null if the set is empty.
For example,
- Create a set of integers.
- Use minOfOrNull() with a selector function that transforms each integer to double.
- Print the resulting minimum value or null.
Kotlin Program
fun main() {
val numbers = setOf(3, 1, 4, 1, 5)
val minValue = numbers.minOfOrNull { it.toDouble() }
println(minValue)
}
Output
1.0
2 Finding the minimum length of strings or null if empty
Using minOfOrNull() to find the minimum length among strings in a set, or null if the set is empty.
For example,
- Create a set of strings.
- Use minOfOrNull() with a selector function that returns the length of each string as a double.
- Print the resulting minimum length or null.
Kotlin Program
fun main() {
val strings = setOf("apple", "pear", "banana")
val minLength = strings.minOfOrNull { it.length.toDouble() }
println(minLength)
}
Output
4.0
3 Finding the minimum age among custom objects or null if empty
Using minOfOrNull() to find the minimum age among custom objects in a set, or null if the set is empty.
For example,
- Create a data class.
- Create a set of custom objects.
- Use minOfOrNull() with a selector function that returns the age of each object as a double.
- 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.minOfOrNull { it.age.toDouble() }
println(minAge)
}
Output
25.0
4 Handling an empty set
Using minOfOrNull() to handle an empty set and return null.
For example,
- Create an empty set of integers.
- Use minOfOrNull() with a selector function that transforms each integer to double.
- Print the resulting minimum value or null.
Kotlin Program
fun main() {
val emptySet = emptySet<Int>()
val minValue = emptySet.minOfOrNull { it.toDouble() }
println(minValue)
}
Output
null
Summary
In this Kotlin tutorial, we learned about minOfOrNull() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.