max

@JvmName(name = "maxOrThrow")
fun Array<out Double>.max(): Double(source)
@JvmName(name = "maxOrThrow")
fun Array<out Float>.max(): Float(source)
@JvmName(name = "maxOrThrow")
fun FloatArray.max(): Float(source)
@JvmName(name = "maxOrThrow")
fun DoubleArray.max(): Double(source)

Returns the largest element.

If any of elements is NaN, this function returns NaN.

Since Kotlin

1.7

Throws

if the array is empty.

Samples

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

fun main() { 
   //sampleStart 
   // The largest and smallest elements in the array
val numbers = doubleArrayOf(3.0, 7.2, 2.4, 6.5)
println(numbers.max()) // 7.2
println(numbers.min()) // 2.4

// max() and min() return `NaN` if any of elements is `NaN`
val numbersWithNaN = doubleArrayOf(3.0, Double.NaN, 7.2, 2.4, 6.5)
println(numbersWithNaN.max()) // NaN
println(numbersWithNaN.min()) // NaN

val emptyArray = doubleArrayOf()

// max() and min() throw if the array is empty
// emptyArray.max() // will fail with NoSuchElementException
// emptyArray.min() // will fail with NoSuchElementException

// maxOrNull() and minOrNull() return null if the array is empty
println(emptyArray.maxOrNull()) // null
println(emptyArray.minOrNull()) // null 
   //sampleEnd
}

@JvmName(name = "maxOrThrow")
fun <T : Comparable<T>> Array<out T>.max(): T(source)

Returns the largest element.

If there are multiple equal maximal elements, this function returns the first of those elements.

Since Kotlin

1.7

Throws

if the array is empty.

Samples

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

fun main() { 
   //sampleStart 
   // The largest and smallest elements according to String.compareTo
val names = listOf("Alice", "Bob", "Carol")
println(names.max()) // Carol
println(names.min()) // Alice

val emptyList = emptyList<Int>()

// max() and min() throw if the collection is empty
// emptyList.max() // will fail with NoSuchElementException
// emptyList.min() // will fail with NoSuchElementException

// maxOrNull() and minOrNull() return null if the collection is empty
println(emptyList.maxOrNull()) // null
println(emptyList.minOrNull()) // null 
   //sampleEnd
}

@JvmName(name = "maxOrThrow")
fun ByteArray.max(): Byte(source)
@JvmName(name = "maxOrThrow")
fun ShortArray.max(): Short(source)
@JvmName(name = "maxOrThrow")
fun IntArray.max(): Int(source)
@JvmName(name = "maxOrThrow")
fun LongArray.max(): Long(source)
@JvmName(name = "maxOrThrow")
fun CharArray.max(): Char(source)
@JvmName(name = "maxOrThrow-U")
fun UIntArray.max(): UInt(source)

Returns the largest element.

Since Kotlin

1.7

Throws

if the array is empty.

Samples

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

fun main() { 
   //sampleStart 
   // The largest and smallest elements in the array
val numbers = intArrayOf(3, 7, 2, 6)
println(numbers.max()) // 7
println(numbers.min()) // 2

val emptyArray = intArrayOf()

// max() and min() throw if the array is empty
// emptyArray.max() // will fail with NoSuchElementException
// emptyArray.min() // will fail with NoSuchElementException

// maxOrNull() and minOrNull() return null if the array is empty
println(emptyArray.maxOrNull()) // null
println(emptyArray.minOrNull()) // null 
   //sampleEnd
}

@JvmName(name = "maxOrThrow")
fun Iterable<Double>.max(): Double(source)
@JvmName(name = "maxOrThrow")
fun Iterable<Float>.max(): Float(source)

Returns the largest element.

If any of elements is NaN, this function returns NaN.

Since Kotlin

1.7

Throws

if the collection is empty.

Samples

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

fun main() { 
   //sampleStart 
   // The largest and smallest elements in the array
val numbers = doubleArrayOf(3.0, 7.2, 2.4, 6.5)
println(numbers.max()) // 7.2
println(numbers.min()) // 2.4

// max() and min() return `NaN` if any of elements is `NaN`
val numbersWithNaN = doubleArrayOf(3.0, Double.NaN, 7.2, 2.4, 6.5)
println(numbersWithNaN.max()) // NaN
println(numbersWithNaN.min()) // NaN

val emptyArray = doubleArrayOf()

// max() and min() throw if the array is empty
// emptyArray.max() // will fail with NoSuchElementException
// emptyArray.min() // will fail with NoSuchElementException

// maxOrNull() and minOrNull() return null if the array is empty
println(emptyArray.maxOrNull()) // null
println(emptyArray.minOrNull()) // null 
   //sampleEnd
}

@JvmName(name = "maxOrThrow")
fun <T : Comparable<T>> Iterable<T>.max(): T(source)

Returns the largest element.

If there are multiple equal maximal elements, this function returns the first of those elements.

Since Kotlin

1.7

Throws

if the collection is empty.

Samples

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

fun main() { 
   //sampleStart 
   // The largest and smallest elements according to String.compareTo
val names = listOf("Alice", "Bob", "Carol")
println(names.max()) // Carol
println(names.min()) // Alice

val emptyList = emptyList<Int>()

// max() and min() throw if the collection is empty
// emptyList.max() // will fail with NoSuchElementException
// emptyList.min() // will fail with NoSuchElementException

// maxOrNull() and minOrNull() return null if the collection is empty
println(emptyList.maxOrNull()) // null
println(emptyList.minOrNull()) // null 
   //sampleEnd
}

Deprecated

Warning since 1.4

Error since 1.5

Hidden since 1.6

Use maxOrNull instead.

Replace with

this.maxOrNull()

Since Kotlin

1.1

fun <T : Comparable<T>> Array<out T>.max(): T?(source)
fun <T : Comparable<T>> Iterable<T>.max(): T?(source)

Deprecated

Warning since 1.4

Error since 1.5

Hidden since 1.6

Use maxOrNull instead.

Replace with

this.maxOrNull()

Since Kotlin

1.0

Deprecated

Warning since 1.4

Error since 1.5

Hidden since 1.6

Use maxOrNull instead.

Replace with

this.maxOrNull()

Since Kotlin

1.3