Kotlin Map collection – map() methods

In the tutorial, JavaSampleApproach will show you how to use map() methods to transform Kotlin Map collection to Kotlin List collection or a new Map collection.

I. Kotlin Map collection with map() methods

Kotlin Map collection supports a set of map() methods to transform the given map to a new map or a new list:

Now practicing ->

0. Initial data for practicing

1. Transform a given Kotlin Map to List
1.1 map()

Method signature:

-> Returns a list containing the results of applying the given [transform] function to each entry in the original map.

Practice:

1.2 mapNotNull()

Method signature:

-> Returns a list containing only the non-null results of applying the given [transform] function to each entry in the original map.

Practice:

1.3 mapTo()

Method signature:

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

Practice:

1.4 mapNotNullTo()

Method signature:

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

Practice:

2. Transform a given Kotlin Map to new Map
2.1 mapKeys()

Method signature:

-> Returns a new Map with entries having the keys obtained by applying the [transform] function to each entry in this [Map] and the values of this map.

Practice:

2.2 mapKeysTo()

Method signature:

-> Populates the given [destination] map with entries having the keys obtained by applying the [transform] function to each entry in this [Map] and the values of this map.

Practice:

2.3 mapValues()

Method signature:

-> Returns a new map with entries having the keys of this map and the values obtained by applying the [transform] function to each entry in this [Map].

Practice:

2.4 mapValuesTo()

Method signature:

-> Populates the given [destination] map with entries having the keys of this map and the values obtained by applying the [transform] function to each entry in this [Map].

Practice:

II. Full sourcecode

By JavaSampleApproach | January 24, 2018.

Related Posts


Got Something To Say:

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

*