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
Parameter | Optional/Required | Description |
---|---|---|
selector | required | A 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,
- Create a set of integers.
- Use maxOfOrNull() with a selector function that transforms each integer to double.
- 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,
- Create a set of strings.
- Use maxOfOrNull() with a selector function that returns the length of each string as a double.
- 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,
- Create a data class.
- Create a set of custom objects.
- Use maxOfOrNull() with a selector function that returns the age of each object as a double.
- 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,
- Create an empty set of integers.
- Use maxOfOrNull() with a selector function that transforms each integer to double.
- 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.