maxBy

@JvmName(name = "maxByOrThrow")
inline fun <T, R : Comparable<R>> Array<out T>.maxBy(selector: (T) -> R): T(source)
@JvmName(name = "maxByOrThrow")
inline fun <R : Comparable<R>> ByteArray.maxBy(selector: (Byte) -> R): Byte(source)
@JvmName(name = "maxByOrThrow")
inline fun <R : Comparable<R>> ShortArray.maxBy(selector: (Short) -> R): Short(source)
@JvmName(name = "maxByOrThrow")
inline fun <R : Comparable<R>> IntArray.maxBy(selector: (Int) -> R): Int(source)
@JvmName(name = "maxByOrThrow")
inline fun <R : Comparable<R>> LongArray.maxBy(selector: (Long) -> R): Long(source)
@JvmName(name = "maxByOrThrow")
inline fun <R : Comparable<R>> FloatArray.maxBy(selector: (Float) -> R): Float(source)
@JvmName(name = "maxByOrThrow")
inline fun <R : Comparable<R>> DoubleArray.maxBy(selector: (Double) -> R): Double(source)
@JvmName(name = "maxByOrThrow")
inline fun <R : Comparable<R>> BooleanArray.maxBy(selector: (Boolean) -> R): Boolean(source)
@JvmName(name = "maxByOrThrow")
inline fun <R : Comparable<R>> CharArray.maxBy(selector: (Char) -> R): Char(source)
@JvmName(name = "maxByOrThrow-U")
inline fun <R : Comparable<R>> UIntArray.maxBy(selector: (UInt) -> R): UInt(source)
@JvmName(name = "maxByOrThrow-U")
inline fun <R : Comparable<R>> ULongArray.maxBy(selector: (ULong) -> R): ULong(source)
@JvmName(name = "maxByOrThrow-U")
inline fun <R : Comparable<R>> UByteArray.maxBy(selector: (UByte) -> R): UByte(source)
@JvmName(name = "maxByOrThrow-U")
inline fun <R : Comparable<R>> UShortArray.maxBy(selector: (UShort) -> R): UShort(source)

Returns the first element yielding the largest value of the given selector function.

If there are multiple equal maximal values returned by the selector function, this function returns the first of elements corresponding to these values.

Note that the function selector is not invoked when the array contains zero or one elements because in these cases it is clear which element to return without invoking the selector. Therefore it's recommended to avoid relying on side effects being performed by the selector function on each element.

Since Kotlin

1.7

Throws

if the array is empty.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val strings = listOf("abcd", "abc", "ab", "de", "abcde")

val longestString = strings.maxBy { it.length }
println(longestString) // abcde

val shortestString = strings.minBy { it.length }
println(shortestString) // ab

val emptyList = emptyList<String>()
// maxBy() and minBy() throw if the collection is empty
// emptyList.maxBy { it.length } // will fail with NoSuchElementException
// emptyList.minBy { it.length } // will fail with NoSuchElementException
// maxByOrNull() and minByOrNull() return null if the collection is empty
println(emptyList.maxByOrNull { it.length }) // null
println(emptyList.minByOrNull { it.length }) // null 
   //sampleEnd
}

@JvmName(name = "maxByOrThrow")
inline fun <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R): T(source)

Returns the first element yielding the largest value of the given selector function.

If there are multiple equal maximal values returned by the selector function, this function returns the first of elements corresponding to these values.

Note that the function selector is not invoked when the collection contains zero or one elements because in these cases it is clear which element to return without invoking the selector. Therefore it's recommended to avoid relying on side effects being performed by the selector function on each element.

Since Kotlin

1.7

Throws

if the collection is empty.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val strings = listOf("abcd", "abc", "ab", "de", "abcde")

val longestString = strings.maxBy { it.length }
println(longestString) // abcde

val shortestString = strings.minBy { it.length }
println(shortestString) // ab

val emptyList = emptyList<String>()
// maxBy() and minBy() throw if the collection is empty
// emptyList.maxBy { it.length } // will fail with NoSuchElementException
// emptyList.minBy { it.length } // will fail with NoSuchElementException
// maxByOrNull() and minByOrNull() return null if the collection is empty
println(emptyList.maxByOrNull { it.length }) // null
println(emptyList.minByOrNull { it.length }) // null 
   //sampleEnd
}

@JvmName(name = "maxByOrThrow")
inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxBy(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>(source)

Returns the first entry yielding the largest value of the given selector function.

If there are multiple equal maximal values returned by the selector function, this function returns the first of entries corresponding to these values.

Note that the function selector is not invoked when the map contains zero or one entries because in these cases it is clear which entry to return without invoking the selector. Therefore it's recommended to avoid relying on side effects being performed by the selector function on each entry.

Since Kotlin

1.7

Throws

if the map is empty.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val strings = listOf("abcd", "abc", "ab", "de", "abcde")

val longestString = strings.maxBy { it.length }
println(longestString) // abcde

val shortestString = strings.minBy { it.length }
println(shortestString) // ab

val emptyList = emptyList<String>()
// maxBy() and minBy() throw if the collection is empty
// emptyList.maxBy { it.length } // will fail with NoSuchElementException
// emptyList.minBy { it.length } // will fail with NoSuchElementException
// maxByOrNull() and minByOrNull() return null if the collection is empty
println(emptyList.maxByOrNull { it.length }) // null
println(emptyList.minByOrNull { it.length }) // null 
   //sampleEnd
}
inline fun <T, R : Comparable<R>> Array<out T>.maxBy(selector: (T) -> R): T?(source)
inline fun <R : Comparable<R>> ByteArray.maxBy(selector: (Byte) -> R): Byte?(source)
inline fun <R : Comparable<R>> ShortArray.maxBy(selector: (Short) -> R): Short?(source)
inline fun <R : Comparable<R>> IntArray.maxBy(selector: (Int) -> R): Int?(source)
inline fun <R : Comparable<R>> LongArray.maxBy(selector: (Long) -> R): Long?(source)
inline fun <R : Comparable<R>> FloatArray.maxBy(selector: (Float) -> R): Float?(source)
inline fun <R : Comparable<R>> DoubleArray.maxBy(selector: (Double) -> R): Double?(source)
inline fun <R : Comparable<R>> BooleanArray.maxBy(selector: (Boolean) -> R): Boolean?(source)
inline fun <R : Comparable<R>> CharArray.maxBy(selector: (Char) -> R): Char?(source)
inline fun <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R): T?(source)
inline fun <K, V, R : Comparable<R>> Map<out K, V>.maxBy(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>?(source)

Deprecated

Warning since 1.4

Error since 1.5

Hidden since 1.6

Use maxByOrNull instead.

Replace with

this.maxByOrNull(selector)

Since Kotlin

1.0

inline fun <R : Comparable<R>> UIntArray.maxBy(selector: (UInt) -> R): UInt?(source)

Deprecated

Warning since 1.4

Error since 1.5

Hidden since 1.6

Use maxByOrNull instead.

Replace with

this.maxByOrNull(selector)

Since Kotlin

1.3