Kotlin Set minWithOrNull()
Syntax & Examples
Set.minWithOrNull() extension function
The minWithOrNull() extension function in Kotlin returns the first element having the smallest value according to the provided comparator, or null if there are no elements.
Syntax of Set.minWithOrNull()
The syntax of Set.minWithOrNull() extension function is:
fun <T> Set<T>.minWithOrNull(comparator: Comparator<in T>): T?
This minWithOrNull() extension function of Set returns the first element having the smallest 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.minWithOrNull() returns value of type T?
.
✐ Examples
1 Finding the minimum element in a set of integers with a custom comparator or null if empty
Using minWithOrNull() to find the minimum 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 minWithOrNull() with a custom comparator to find the minimum element.
- Print the resulting element or null.
Kotlin Program
fun main() {
val numbers = setOf(3, 1, 4, 1, 5)
val minNumber = numbers.minWithOrNull(compareBy { it })
println(minNumber)
}
Output
1
2 Finding the minimum string in a set based on length with a custom comparator or null if empty
Using minWithOrNull() to find the minimum 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 minWithOrNull() with a custom comparator to find the minimum string based on length.
- Print the resulting string or null.
Kotlin Program
fun main() {
val strings = setOf("apple", "pear", "banana")
val shortestString = strings.minWithOrNull(compareBy { it.length })
println(shortestString)
}
Output
pear
3 Finding the minimum custom object in a set based on a property with a custom comparator or null if empty
Using minWithOrNull() to find the minimum 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 minWithOrNull() with a custom comparator to find the minimum 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 youngestPerson = people.minWithOrNull(compareBy { it.age })
println(youngestPerson)
}
Output
Person(name=Bob, age=25)
4 Handling an empty set with a custom comparator
Using minWithOrNull() to handle an empty set and return null with a custom comparator.
For example,
- Create an empty set of integers.
- Use minWithOrNull() with a custom comparator to find the minimum element.
- Print the resulting element or null.
Kotlin Program
fun main() {
val emptySet = emptySet<Int>()
val minNumber = emptySet.minWithOrNull(compareBy { it })
println(minNumber)
}
Output
null
Summary
In this Kotlin tutorial, we learned about minWithOrNull() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.