Kotlin Distinct() methods of List collection example

kotlin-distinct-methods-of-list-collection-feature-image-example

In the tutorial, Grokonez will show you how to work with Kotlin distinct() method of List collection.

I. Practice

1. distinct() method

distinct() method is used to return a list that contains only distinct elements.

Method signature:

public fun <T> Iterable<T>.distinct(): List<T>

– Work with simple list:

val simpleList = listOf(1, 2, 3, 5, 3, 4, 1, 10, 13, 7, 10, 8)
println(simpleList)
// print on console
//->
/*
	[1, 2, 3, 5, 3, 4, 1, 10, 13, 7, 10, 8]
*/

println(simpleList.distinct())
// print on console
// ->
/*
	[1, 2, 3, 5, 4, 10, 13, 7, 8]
*/

– Work with Kotlin objects list:


var customers =  mutableListOf(Customer("Craig", 45),
								Customer("Amos", 23),
								Customer("Jack", 20),
								Customer("Jack", 20))
customers.forEach{println(it)}
// print on console
// ->
/*
	Customer(name=Craig, age=45)
	Customer(name=Amos, age=23)
	Customer(name=Jack, age=20)
	Customer(name=Jack, age=20)
*/

// Distinct Customer
println("----------------------------------")	
val distinctCusts = customers.distinct()
distinctCusts.forEach{println(it)}
// print on console
// ->
/*
	Customer(name=Craig, age=45)
	Customer(name=Amos, age=23)
	Customer(name=Jack, age=20)
*/

2. distinctBy() method

distinctBy() is used to return a list that contains elements having distinct keys returned by the selector function.

Method signature:

public inline fun <T, K> Iterable<T>.distinctBy(selector: (T) -> K): List<T>

Sample code:


customers =  mutableListOf(Customer("Jack", 20),
								Customer("Craig", 45),
								Customer("Amos", 23),
								Customer("Craig", 25))
val distinctByNameCusts = customers.distinctBy{it -> it.name}
distinctByNameCusts.forEach{println(it)}
// print on console
// ->
/*
	Customer(name=Jack, age=20)
	Customer(name=Craig, age=45)
	Customer(name=Amos, age=23)
*/

II. Full sourcecode


package com.javasampleapproach.filterlist

data class Customer(
	val name: String,
	val age: Int
)

fun main(args : Array) {
	val simpleList = listOf(1, 2, 3, 5, 3, 4, 1, 10, 13, 7, 10, 8)
	println(simpleList)
	// print on console
	//->
	/*
		[1, 2, 3, 5, 3, 4, 1, 10, 13, 7, 10, 8]
 	*/
	
	println(simpleList.distinct())
	// print on console
	// ->
	/*
 		[1, 2, 3, 5, 4, 10, 13, 7, 8]
 	*/
	
	//******************************
	//***work with Kotlin Objects***
 	//******************************
	println("----------------------------------")
	var customers =  mutableListOf(Customer("Craig", 45),
									Customer("Amos", 23),
									Customer("Jack", 20),
									Customer("Jack", 20))
	customers.forEach{println(it)}
	// print on console
	// ->
	/*
		Customer(name=Craig, age=45)
		Customer(name=Amos, age=23)
		Customer(name=Jack, age=20)
 		Customer(name=Jack, age=20)
 	*/
	
	// Distinct Customer
	println("----------------------------------")	
	val distinctCusts = customers.distinct()
	distinctCusts.forEach{println(it)}
	// print on console
	// ->
	/*
		Customer(name=Craig, age=45)
		Customer(name=Amos, age=23)
		Customer(name=Jack, age=20)
 	*/
	
	// DistinctByName Customer
	println("----------------------------------")
	customers =  mutableListOf(Customer("Jack", 20),
									Customer("Craig", 45),
									Customer("Amos", 23),
									Customer("Craig", 25))
	val distinctByNameCusts = customers.distinctBy{it -> it.name}
	distinctByNameCusts.forEach{println(it)}
	// print on console
	// ->
	/*
		Customer(name=Jack, age=20)
		Customer(name=Craig, age=45)
		Customer(name=Amos, age=23)
 	*/
}


By grokonez | January 5, 2018.

Last updated on May 7, 2021.



Related Posts


1 thought on “Kotlin Distinct() methods of List collection example”

Got Something To Say:

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

*