Returns the largest value among all values produced by selector function applied to each element in the sequence.
If any of values produced by selector function is NaN
, the returned result is NaN
.
The operation is terminal.
Since Kotlin
1.4Throws
if the sequence is empty.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
data class Rectangle(val width: Double, val height: Double) {
val aspectRatio: Double get() = if (height != 0.0) width / height else Double.NaN
}
val rectangles = listOf(
Rectangle(15.0, 10.0),
Rectangle(25.0, 20.0),
Rectangle(40.0, 30.0),
)
println(rectangles.maxOf { it.aspectRatio })
println(rectangles.minOf { it.aspectRatio })
val rectanglesAndPoint = rectangles + Rectangle(0.0, 0.0)
println(rectanglesAndPoint.maxOf { it.aspectRatio })
println(rectanglesAndPoint.minOf { it.aspectRatio })
val emptyList = emptyList<Rectangle>()
println(emptyList.maxOfOrNull { it.aspectRatio })
println(emptyList.minOfOrNull { it.aspectRatio })
}
Target: JVMRunning on v.2.1.20
Returns the largest value among all values produced by selector function applied to each element in the sequence.
If multiple elements produce the maximal value, this function returns the first of those values.
The operation is terminal.
Since Kotlin
1.4Throws
if the sequence is empty.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
val names = listOf("Alice", "Bob", "Carol")
println(names.maxOf { it.length })
println(names.minOf { it.length })
val emptyList = emptyList<String>()
println(emptyList.maxOfOrNull { it.length })
println(emptyList.minOfOrNull { it.length })
}
Target: JVMRunning on v.2.1.20