DEV Community

loizenai
loizenai

Posted on

Kotlin List methods – min() minBy() minWith()

https://grokonez.com/kotlin/kotlin-list-methods-min-minby-minwith

Kotlin List methods – min() minBy() minWith()

In the tutorial, JavaSampleApproach will show how to use Kotlin List methods min(), minBy(), minWith().

I. Kotlin List methods - min() minBy() minWith()

1. min()

min() is used to return the smallest element of an Iterable. It will return 'null' if there are no element.
Method signature:


// 1.   
public fun Iterable.min(): Double?

// 2.
public fun Iterable.min(): Float?

Practice:


val simpleList = listOf(55.4, 20.0, 99.99, 1.99, 23.0, 34.2, 88.0, 72.1, 61.2, 43.9)

// public fun Iterable.min(): Double?
val smallestElement = simpleList.min()
println(smallestElement)
// ->
/*
    1.99
*/

2. minBy()

minBy() returns the first element having smallest value of the given function selector: (T) -> R or 'null' if there are no elements.
Method signature:

More at:

https://grokonez.com/kotlin/kotlin-list-methods-min-minby-minwith

Kotlin List methods – min() minBy() minWith()

Top comments (0)