Kotlin Set maxWithOrNull()
Syntax & Examples
Set.maxWithOrNull() extension function
The maxWithOrNull() extension function in Kotlin returns the first element having the largest value according to the provided comparator, or null if there are no elements.
Syntax of Set.maxWithOrNull()
The syntax of Set.maxWithOrNull() extension function is:
fun <T> Set<T>.maxWithOrNull(comparator: Comparator<in T>): T?
This maxWithOrNull() extension function of Set returns the first element having the largest value according to the provided comparator or null if there are no elements.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
comparator | required | The comparator used to compare the elements. |
Return Type
Set.maxWithOrNull() returns value of type T?
.
✐ Examples
1 Finding the maximum element in a set of integers with a custom comparator or null if empty
Using maxWithOrNull() to find the maximum element in a set of integers with a custom comparator, or null if the set is empty.
For example,
- Create a set of integers.
- Use maxWithOrNull() with a custom comparator to find the maximum element.
- Print the resulting element or null.
Kotlin Program
fun main() {
val numbers = setOf(1, 2, 3, 4, 5)
val maxNumber = numbers.maxWithOrNull(compareBy { it })
println(maxNumber)
}
Output
5
2 Finding the maximum string in a set based on length with a custom comparator or null if empty
Using maxWithOrNull() to find the maximum string in a set based on length with a custom comparator, or null if the set is empty.
For example,
- Create a set of strings.
- Use maxWithOrNull() with a custom comparator to find the maximum string based on length.
- Print the resulting string or null.
Kotlin Program
fun main() {
val strings = setOf("one", "two", "three")
val maxLengthString = strings.maxWithOrNull(compareBy { it.length })
println(maxLengthString)
}
Output
three
3 Finding the maximum custom object in a set based on a property with a custom comparator or null if empty
Using maxWithOrNull() to find the maximum custom object in a set based on a property with a custom comparator, or null if the set is empty.
For example,
- Create a data class.
- Create a set of custom objects.
- Use maxWithOrNull() with a custom comparator to find the maximum object based on a property.
- Print the resulting object 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 oldestPerson = people.maxWithOrNull(compareBy { it.age })
println(oldestPerson)
}
Output
Person(name=Charlie, age=35)
4 Handling an empty set with a custom comparator
Using maxWithOrNull() to handle an empty set and return null with a custom comparator.
For example,
- Create an empty set of integers.
- Use maxWithOrNull() with a custom comparator to find the maximum element.
- Print the resulting element or null.
Kotlin Program
fun main() {
val emptySet = emptySet<Int>()
val maxNumber = emptySet.maxWithOrNull(compareBy { it })
println(maxNumber)
}
Output
null
Summary
In this Kotlin tutorial, we learned about maxWithOrNull() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.