Kotlin Array find the Largest Element – max() maxBy() maxWith() methods

kotlin-array-find-largest-element-max-maxby-maxwith-methods-feature-image

In the tutorial, JavaSampleApproach will show how to use Kotlin Array methods max(), maxBy(), maxWith() to find the largest element in a Kotlin Array.

I. max() method

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

Method signatures:

Example:

max() with Comparable

-> Implement Comparable for Kotlin Array object.

Method signature:

Example:

II. maxBy() with Selector function

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

Method signature:

Example:

III. maxWith() with Comparator

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

Method signature:

Example:



By grokonez | February 1, 2018.

Last updated on September 15, 2018.



Related Posts


Got Something To Say:

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

*