maxByOrNull

Common
JVM
JS
Native
1.4
inline fun <T, R : Comparable<R>> Array<out T>.maxByOrNull(
    selector: (T) -> R
): T?
inline fun <R : Comparable<R>> ByteArray.maxByOrNull(
    selector: (Byte) -> R
): Byte?
inline fun <R : Comparable<R>> ShortArray.maxByOrNull(
    selector: (Short) -> R
): Short?
inline fun <R : Comparable<R>> IntArray.maxByOrNull(
    selector: (Int) -> R
): Int?
inline fun <R : Comparable<R>> LongArray.maxByOrNull(
    selector: (Long) -> R
): Long?
inline fun <R : Comparable<R>> FloatArray.maxByOrNull(
    selector: (Float) -> R
): Float?
inline fun <R : Comparable<R>> DoubleArray.maxByOrNull(
    selector: (Double) -> R
): Double?
inline fun <R : Comparable<R>> BooleanArray.maxByOrNull(
    selector: (Boolean) -> R
): Boolean?
inline fun <R : Comparable<R>> CharArray.maxByOrNull(
    selector: (Char) -> R
): Char?
inline fun <T, R : Comparable<R>> Iterable<T>.maxByOrNull(
    selector: (T) -> R
): T?
@ExperimentalUnsignedTypes inline fun <R : Comparable<R>> UIntArray.maxByOrNull(
    selector: (UInt) -> R
): UInt?
@ExperimentalUnsignedTypes inline fun <R : Comparable<R>> ULongArray.maxByOrNull(
    selector: (ULong) -> R
): ULong?
@ExperimentalUnsignedTypes inline fun <R : Comparable<R>> UByteArray.maxByOrNull(
    selector: (UByte) -> R
): UByte?
@ExperimentalUnsignedTypes inline fun <R : Comparable<R>> UShortArray.maxByOrNull(
    selector: (UShort) -> R
): UShort?

Returns the first element yielding the largest value of the given function or null if there are no elements.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val nameToAge = listOf("Alice" to 42, "Bob" to 28, "Carol" to 51)
val oldestPerson = nameToAge.maxByOrNull { it.second }
println(oldestPerson) // (Carol, 51)

val emptyList = emptyList<Pair<String, Int>>()
val emptyMax = emptyList.maxByOrNull { it.second }
println(emptyMax) // null
//sampleEnd
}
Common
JVM
JS
Native
1.4
inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxByOrNull(
    selector: (Entry<K, V>) -> R
): Entry<K, V>?

Returns the first entry yielding the largest value of the given function or null if there are no entries.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val nameToAge = listOf("Alice" to 42, "Bob" to 28, "Carol" to 51)
val oldestPerson = nameToAge.maxByOrNull { it.second }
println(oldestPerson) // (Carol, 51)

val emptyList = emptyList<Pair<String, Int>>()
val emptyMax = emptyList.maxByOrNull { it.second }
println(emptyMax) // null
//sampleEnd
}