Kotlin Array find the Smallest Element – min() minBy() minWith() methods

In the tutorial, JavaSampleApproach will show how to use Kotlin Array methods min(), minBy(), minWith() to find the smallest element in a Kotlin Array.

I. min() method

Kotlin Array method min() is used to returns the smallest element or null if there are no elements. If any of elements is NaN, it returns NaN.

Method signatures:

Example:

min() with Comparable

-> Implement Comparable for Kotlin Array object.

Method signature:

Example:

II. minBy() with Selector function

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

Method signature:

Example:

III. minWith() with Comparator

Kotlin Array minWith() method returns the first element having the smallest value according to the provided [comparator]

Method signature:

Example:

By JavaSampleApproach | February 1, 2018.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*