Kotlin transform List with map() methods example

In the tutorial, JavaSampleApproach will show you how to transform Kotlin List with map() methods.

I. Kotlin map() methods

Initial data for practicing:

1. map(transform: (T) -> R): List

Method signature:

-> Transform the given list by applying the given [transform] function to each element in the original collection then return a list.

Practice:

2. mapTo(destination: C, transform: (T) -> R): C

Method signature:

-> Applies the given [transform] function to each element of the original collection and appends the results to the given [destination].

Practice:

3. mapNotNull(transform: (T) -> R?): List

Method signature:

-> Transform the given list by applying the given [transform] function to each element in the original collection then returns a list containing only the non-null results.

Practice:

4. mapNotNullTo(destination: C, transform: (T) -> R?): C

Method signature:

-> Applies the given [transform] function to each element in the original collection and appends only the non-null results to the given [destination].

Practice:

5. mapIndexed(transform: (index: Int, T) -> R): List

Method signature:

-> Apply the given [transform] function to each element and its index in the original collection. [transform] function that takes the index of an element and the element itself and returns the result of the transform applied to the element.

Practice:

6. mapIndexedTo(destination: C, transform: (index: Int, T) -> R): C

Method signature:

-> Use the given [transform] function to apply to each element and its index in the original collection and appends the results to the given [destination].

Practice:

7. mapIndexedNotNull(transform: (index: Int, T) -> R?): List

Method signature:

-> Applying the given [transform] function to each element and its index in the original collection then returns a non-null list.

Practice:

8. mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?): C

Method signature:

-> Applies the given [transform] function to each element and its index in the original collection and appends only the non-null results to the given [destination].

Practice:

II. Full Sourcecode

By JavaSampleApproach | January 21, 2018.

Related Posts


Got Something To Say:

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

*