Kotlin Set maxOfWithOrNull()
Syntax & Examples
Set.maxOfWithOrNull() extension function
The maxOfWithOrNull() extension function in Kotlin returns the largest 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.maxOfWithOrNull()
The syntax of Set.maxOfWithOrNull() extension function is:
fun <T, R> Set<T>.maxOfWithOrNull(comparator: Comparator<in R>, selector: (T) -> R): R?
This maxOfWithOrNull() extension function of Set returns the largest 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
Parameter | Optional/Required | Description |
---|---|---|
comparator | required | The comparator used to compare the values. |
selector | required | A function that takes an element and returns a value to be compared. |
Return Type
Set.maxOfWithOrNull() returns value of type R?
.
✐ Examples
1 Finding the maximum value among integers with a custom comparator or null if empty
Using maxOfWithOrNull() to find the maximum value among integers in a set with a custom comparator, or null if the set is empty.
For example,
- Create a set of integers.
- Use maxOfWithOrNull() with a custom comparator and a selector function that transforms each integer.
- Print the resulting maximum value or null.
Kotlin Program
fun main() {
val numbers = setOf(1, 2, 3, 4, 5)
val maxValue = numbers.maxOfWithOrNull(compareBy { it }, { it.toDouble() })
println(maxValue)
}
Output
5.0
2 Finding the maximum length of strings with a custom comparator or null if empty
Using maxOfWithOrNull() to find the maximum length among strings in a set with a custom comparator, or null if the set is empty.
For example,
- Create a set of strings.
- Use maxOfWithOrNull() with a custom comparator and a selector function that returns the length of each string.
- Print the resulting maximum length or null.
Kotlin Program
fun main() {
val strings = setOf("one", "two", "three")
val maxLength = strings.maxOfWithOrNull(compareBy { it }, { it.length.toDouble() })
println(maxLength)
}
Output
5.0
3 Finding the maximum age among custom objects with a custom comparator or null if empty
Using maxOfWithOrNull() to find the maximum age among custom objects in a set with a custom comparator, or null if the set is empty.
For example,
- Create a data class.
- Create a set of custom objects.
- Use maxOfWithOrNull() with a custom comparator and a selector function that returns the age of each object.
- 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.maxOfWithOrNull(compareBy { it }, { it.age.toDouble() })
println(maxAge)
}
Output
35.0
4 Handling an empty set with a custom comparator
Using maxOfWithOrNull() to handle an empty set and return null with a custom comparator.
For example,
- Create an empty set of integers.
- Use maxOfWithOrNull() with a custom comparator and 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.maxOfWithOrNull(compareBy { it }, { it.toDouble() })
println(maxValue)
}
Output
null
Summary
In this Kotlin tutorial, we learned about maxOfWithOrNull() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.