Kotlin filter List example

kotlin-list-filter-methods-example-feature-image

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:

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

Practice:

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

Method signature:

-> Appends all elements matching the given [predicate] to the given [destination].

Practice:

3. filterNotTo(destination: C, predicate: (T) -> Boolean)

Method signature:

-> Appends all elements not matching the given [predicate] to the given [destination].

Practice:

4. filterIndexed(predicate: (index: Int, T) -> Boolean)

Method signature:

-> Returns a list containing only elements matching the given [predicate]. [predicate] function uses the element and its index and returns the result of predicate evaluation on the element.

Practice:

5. filterIndexedTo(destination: C, predicate: (index: Int, T) -> Boolean)

Method signature:

-> Appends all elements matching the given [predicate] to the given [destination]. [predicate] function uses the element and its index to evalute the result of predicate on the element.

Practice:

6. filterIsInstance()

Method signature:

-> Returns a list containing all elements with specified type parameter R

Practice:

7. filterIsInstanceTo(destination: C)

Method signature:

-> Appends all elements that are instances of specified type parameter R to the given [destination]

Practice:

8. filterNot(predicate: (T) -> Boolean)

Method signature:

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

Practice:

9. filterNotNull()

Method signature:

-> Returns a list containing all elements that are not null.

Practice:

10. filterNotNullTo(destination: C)

Method signature:

-> Appends all elements that are not ‘null’ to the given [destination]

Practice:

II. Full Sourcecode



By grokonez | January 20, 2018.

Last updated on September 10, 2018.



Related Posts


1 thought on “Kotlin filter List example”

Got Something To Say:

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

*