DEV Community

loading...

Kotlin Distinct() methods of List collection example

loizenai profile image loizenai ・1 min read

https://grokonez.com/kotlin/kotlin-distinct-methods-list-collection

Kotlin Distinct() methods of List collection 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:

More at:

https://grokonez.com/kotlin/kotlin-distinct-methods-list-collection

Kotlin Distinct() methods of List collection example

Discussion (0)

Forem Open with the Forem app