maxOf
Returns the largest 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
if the array is empty.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
data class Rectangle(val width: Double, val height: Double) {
val aspectRatio: Double get() = if (height != 0.0) width / height else Double.NaN
}
// The largest and smallest width-to-height ratios
val rectangles = listOf(
Rectangle(15.0, 10.0),
Rectangle(25.0, 20.0),
Rectangle(40.0, 30.0),
)
println(rectangles.maxOf { it.aspectRatio }) // 1.5
println(rectangles.minOf { it.aspectRatio }) // 1.25
// Aspect ratio of a point (0.0 by 0.0) is NaN, hence the result is NaN
val rectanglesAndPoint = rectangles + Rectangle(0.0, 0.0)
println(rectanglesAndPoint.maxOf { it.aspectRatio }) // NaN
println(rectanglesAndPoint.minOf { it.aspectRatio }) // NaN
val emptyList = emptyList<Rectangle>()
// maxOf() and minOf() throw if the collection is empty
// emptyList.maxOf { it.aspectRatio } // will fail with NoSuchElementException
// emptyList.minOf { it.aspectRatio } // will fail with NoSuchElementException
// maxOfOrNull() and minOfOrNull() return null if the collection is empty
println(emptyList.maxOfOrNull { it.aspectRatio }) // null
println(emptyList.minOfOrNull { it.aspectRatio }) // null
//sampleEnd
}
Returns the largest value among all values produced by selector function applied to each element in the array.
If multiple elements produce the maximal value, this function returns the first of those values.
Since Kotlin
1.4Throws
if the array is empty.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// The length of the longest and shortest names
val names = listOf("Alice", "Bob", "Carol")
println(names.maxOf { it.length }) // 5
println(names.minOf { it.length }) // 3
val emptyList = emptyList<String>()
// maxOf() and minOf() throw if the collection is empty
// emptyList.maxOf { it.length } // will fail with NoSuchElementException
// emptyList.minOf { it.length } // will fail with NoSuchElementException
// maxOfOrNull() and minOfOrNull() return null if the collection is empty
println(emptyList.maxOfOrNull { it.length }) // null
println(emptyList.minOfOrNull { it.length }) // null
//sampleEnd
}
Returns the largest value among all values produced by selector function applied to each element in the array.
If multiple elements produce the maximal value, this function returns the first of those values.
Since Kotlin
1.4Throws
if the array is empty.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// The largest and smallest last digits in the array
val numbers = intArrayOf(13, 7, 22, 64)
println(numbers.maxOf { it % 10 }) // 7
println(numbers.minOf { it % 10 }) // 2
val emptyArray = intArrayOf()
// maxOf() and minOf() throw if the array is empty
// emptyArray.maxOf { it % 10 } // will fail with NoSuchElementException
// emptyArray.minOf { it % 10 } // will fail with NoSuchElementException
// maxOfOrNull() and minOfOrNull() return null if the array is empty
println(emptyArray.maxOfOrNull { it % 10 }) // null
println(emptyArray.minOfOrNull { it % 10 }) // null
//sampleEnd
}
Returns the largest 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() {
//sampleStart
data class Rectangle(val width: Double, val height: Double) {
val aspectRatio: Double get() = if (height != 0.0) width / height else Double.NaN
}
// The largest and smallest width-to-height ratios
val rectangles = listOf(
Rectangle(15.0, 10.0),
Rectangle(25.0, 20.0),
Rectangle(40.0, 30.0),
)
println(rectangles.maxOf { it.aspectRatio }) // 1.5
println(rectangles.minOf { it.aspectRatio }) // 1.25
// Aspect ratio of a point (0.0 by 0.0) is NaN, hence the result is NaN
val rectanglesAndPoint = rectangles + Rectangle(0.0, 0.0)
println(rectanglesAndPoint.maxOf { it.aspectRatio }) // NaN
println(rectanglesAndPoint.minOf { it.aspectRatio }) // NaN
val emptyList = emptyList<Rectangle>()
// maxOf() and minOf() throw if the collection is empty
// emptyList.maxOf { it.aspectRatio } // will fail with NoSuchElementException
// emptyList.minOf { it.aspectRatio } // will fail with NoSuchElementException
// maxOfOrNull() and minOfOrNull() return null if the collection is empty
println(emptyList.maxOfOrNull { it.aspectRatio }) // null
println(emptyList.minOfOrNull { it.aspectRatio }) // null
//sampleEnd
}
Returns the largest value among all values produced by selector function applied to each element in the collection.
If multiple elements produce the maximal 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() {
//sampleStart
// The length of the longest and shortest names
val names = listOf("Alice", "Bob", "Carol")
println(names.maxOf { it.length }) // 5
println(names.minOf { it.length }) // 3
val emptyList = emptyList<String>()
// maxOf() and minOf() throw if the collection is empty
// emptyList.maxOf { it.length } // will fail with NoSuchElementException
// emptyList.minOf { it.length } // will fail with NoSuchElementException
// maxOfOrNull() and minOfOrNull() return null if the collection is empty
println(emptyList.maxOfOrNull { it.length }) // null
println(emptyList.minOfOrNull { it.length }) // null
//sampleEnd
}
Returns the largest 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
if the map is empty.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
data class Rectangle(val width: Double, val height: Double) {
val aspectRatio: Double get() = if (height != 0.0) width / height else Double.NaN
}
// The largest and smallest width-to-height ratios
val rectangles = listOf(
Rectangle(15.0, 10.0),
Rectangle(25.0, 20.0),
Rectangle(40.0, 30.0),
)
println(rectangles.maxOf { it.aspectRatio }) // 1.5
println(rectangles.minOf { it.aspectRatio }) // 1.25
// Aspect ratio of a point (0.0 by 0.0) is NaN, hence the result is NaN
val rectanglesAndPoint = rectangles + Rectangle(0.0, 0.0)
println(rectanglesAndPoint.maxOf { it.aspectRatio }) // NaN
println(rectanglesAndPoint.minOf { it.aspectRatio }) // NaN
val emptyList = emptyList<Rectangle>()
// maxOf() and minOf() throw if the collection is empty
// emptyList.maxOf { it.aspectRatio } // will fail with NoSuchElementException
// emptyList.minOf { it.aspectRatio } // will fail with NoSuchElementException
// maxOfOrNull() and minOfOrNull() return null if the collection is empty
println(emptyList.maxOfOrNull { it.aspectRatio }) // null
println(emptyList.minOfOrNull { it.aspectRatio }) // null
//sampleEnd
}
Returns the largest value among all values produced by selector function applied to each entry in the map.
If multiple entries produce the maximal value, this function returns the first of those values.
Since Kotlin
1.4Throws
if the map is empty.
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// The length of the longest and shortest names
val names = listOf("Alice", "Bob", "Carol")
println(names.maxOf { it.length }) // 5
println(names.minOf { it.length }) // 3
val emptyList = emptyList<String>()
// maxOf() and minOf() throw if the collection is empty
// emptyList.maxOf { it.length } // will fail with NoSuchElementException
// emptyList.minOf { it.length } // will fail with NoSuchElementException
// maxOfOrNull() and minOfOrNull() return null if the collection is empty
println(emptyList.maxOfOrNull { it.length }) // null
println(emptyList.minOfOrNull { it.length }) // null
//sampleEnd
}