Kotlin List methods – max() maxBy() maxWith() example

kotlin-list-methods-max-maxby-maxwith-feature-image

In the tutorial, JavaSampleApproach will show how to use Kotlin List methods max(), maxBy(), maxWith().


Reactjs Jwt SpringBoot Token Authentication Example
Spring Boot Angular 11 Pagination Example
Angular Client Side Pagination with Nodejs + MySQL
Kotlin Tutorial

I. Kotlin List methods – max() maxBy() maxWith()

1. max()

max() is used to return the largest element of an Iterable. It will return ‘null’ if there are no element.
Method signature:


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

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

Practice:


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

// public fun Iterable.max(): Double?
val largestElement = simpleList.max()
println(largestElement)
// ->
/*
	99.99
*/

2. maxBy()

maxBy() returns the first element having largest value of the given function selector: (T) -> R or ‘null’ if there are no elements.
Method signature:


public inline fun > Iterable.maxBy(selector: (T) -> R): T?

Practice:


val productList = listOf(Product("Samsung Galaxy S8 64GB Unlocked Phone", 699.99),
						Product("iPad Pro 9.7-inch 32 GB", 474.98),
						Product("Samsung Electronics Ultra HD Smart LED TV", 677.92),
						Product("Google Pixel Phone - 5 inch display", 279.95),
						Product("iPad Pro 9.7-inch 128G", 574.99),
						Product("Google Wifi system 1-Pack", 149.90),
						Product("iPhone 8 Plus 64G", 850.00),
						Product("Samsung Galaxy Tab 4", 127.67))

// public inline fun > Iterable.maxBy(selector: (T) -> R): T?
var productWithHighestPrice = productList.maxBy { it ->  it.price }

println(productWithHighestPrice)
// ->
/*
	Product(name=iPhone 8 Plus 64G, price=850.0)
*/

3. maxWith()

maxWith() returns the first element having the largest value according to the provided [comparator]
Method signature:


public fun  Iterable.maxWith(comparator: Comparator): T?

Practice:


val productList = listOf(Product("Samsung Galaxy S8 64GB Unlocked Phone", 699.99),
						Product("iPad Pro 9.7-inch 32 GB", 474.98),
						Product("Samsung Electronics Ultra HD Smart LED TV", 677.92),
						Product("Google Pixel Phone - 5 inch display", 279.95),
						Product("iPad Pro 9.7-inch 128G", 574.99),
						Product("Google Wifi system 1-Pack", 149.90),
						Product("iPhone 8 Plus 64G", 850.00),
						Product("Samsung Galaxy Tab 4", 127.67))
						
...

// public fun  Iterable.maxWith(comparator: Comparator): T?
productWithHighestPrice = productList.maxWith(object: Comparator {
	override fun compare(p1: Product, p2: Product): Int = when {
		p1.price > p2.price -> 1
		p1.price == p2.price -> 0
		else -> -1
	}
})

println(productWithHighestPrice)
// ->
/*
	Product(name=iPhone 8 Plus 64G, price=850.0)
*/

II. Full Sourcecode


package com.javasampleapproach.kotlin.maxvalue

data class Product(
	val name: String,
	val price: Double // usd
)

fun main(args : Array) {
	val simpleList = listOf(1.99, 55.4, 20.0, 99.99, 23.0, 34.2, 88.0, 72.1, 61.2, 43.9)
	
	// public fun Iterable.max(): Double?
	val largestElement = simpleList.max()
	println(largestElement)
	// ->
	/*
		99.99
	*/
	
	val productList = listOf(Product("Samsung Galaxy S8 64GB Unlocked Phone", 699.99),
							Product("iPad Pro 9.7-inch 32 GB", 474.98),
							Product("Samsung Electronics Ultra HD Smart LED TV", 677.92),
							Product("Google Pixel Phone - 5 inch display", 279.95),
							Product("iPad Pro 9.7-inch 128G", 574.99),
							Product("Google Wifi system 1-Pack", 149.90),
							Product("iPhone 8 Plus 64G", 850.00),
							Product("Samsung Galaxy Tab 4", 127.67))

	// public inline fun > Iterable.maxBy(selector: (T) -> R): T?
	var productWithHighestPrice = productList.maxBy { it ->  it.price }
	
	println(productWithHighestPrice)
	// ->
	/*
		Product(name=iPhone 8 Plus 64G, price=850.0)
	*/
	
	// public fun  Iterable.maxWith(comparator: Comparator): T?
	productWithHighestPrice = productList.maxWith(object: Comparator {
		override fun compare(p1: Product, p2: Product): Int = when {
			p1.price > p2.price -> 1
			p1.price == p2.price -> 0
			else -> -1
		}
	})
	
	println(productWithHighestPrice)
	// ->
	/*
		Product(name=iPhone 8 Plus 64G, price=850.0)
	*/
}


By grokonez | January 10, 2018.

Last updated on April 19, 2021.



Related Posts


Got Something To Say:

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

*