minByOrNull
Returns the first element yielding the smallest value of the given selector function or null
if there are no elements.
If there are multiple equal minimal 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.4Samples
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
}
Returns the first element yielding the smallest value of the given selector function or null
if there are no elements.
If there are multiple equal minimal 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.4Samples
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
}
Returns the first entry yielding the smallest value of the given selector function or null
if there are no entries.
If there are multiple equal minimal 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.4Samples
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
}