Returns the smallest value among all values produced by selector function applied to each element in the array.
If any of values produced by selector function is NaN
, the returned result is NaN
.
Since Kotlin
1.4Throws
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 smallest value among all values produced by selector function applied to each element in the array.
If multiple elements produce the minimal value, this function returns the first of those values.
Since Kotlin
1.4Throws
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
Returns the smallest value among all values produced by selector function applied to each element in the array.
If multiple elements produce the minimal value, this function returns the first of those values.
Since Kotlin
1.4Throws
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
val numbers = intArrayOf(13, 7, 22, 64)
println(numbers.maxOf { it % 10 })
println(numbers.minOf { it % 10 })
val emptyArray = intArrayOf()
println(emptyArray.maxOfOrNull { it % 10 })
println(emptyArray.minOfOrNull { it % 10 })
}
Target: JVMRunning on v.2.1.20
Returns the smallest value among all values produced by selector function applied to each element in the collection.
If any of values produced by selector function is NaN
, the returned result is NaN
.
Since Kotlin
1.4Throws
if the collection 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 smallest value among all values produced by selector function applied to each element in the collection.
If multiple elements produce the minimal value, this function returns the first of those values.
Since Kotlin
1.4Throws
if the collection 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
Returns the smallest value among all values produced by selector function applied to each entry in the map.
If any of values produced by selector function is NaN
, the returned result is NaN
.
Since Kotlin
1.4Throws
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 smallest value among all values produced by selector function applied to each entry in the map.
If multiple entries produce the minimal value, this function returns the first of those values.
Since Kotlin
1.4Throws
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