DEV Community

loading...

Kotlin filter List example

loizenai profile image loizenai ・1 min read

https://grokonez.com/kotlin/kotlin-list-filter-methods-example

Kotlin filter List example

In the tutorial, Grokonez will show you how to work with Kotlin List filter methods.

I. Kotlin List filter

1. filter(predicate: (T) -> Boolean): List

Method signature:

public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T>

-> Returns a list containing only elements matching the given [predicate]

Practice:


val simpleList = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
var filterList = simpleList.filter { it%2 == 0 }
println(filterList)
/*
    [2, 4, 6, 8, 10]
*/

2. filterTo(destination: C, predicate: (T) -> Boolean)

Method signature:

More at:

https://grokonez.com/kotlin/kotlin-list-filter-methods-example

Kotlin filter List example

Discussion (0)

Forem Open with the Forem app