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:

1. fun <K, V, R> Map<out K, V>.map(transform: (Map.Entry<K, V>) -> R): List<R>
2. fun <K, V, R : Any> Map<out K, V>.mapNotNull(transform: (Map.Entry<K, V>) -> R?): List<R>
3. fun <K, V, R, C : MutableCollection<in R>> Map<out K, V>.mapTo(destination: C, transform: (Map.Entry<K, V>) -> R): C
4. fun <K, V, R : Any, C : MutableCollection<in R>> Map<out K, V>.mapNotNullTo(destination: C, transform: (Map.Entry<K, V>) -> R?): C
5. fun <K, V, R> Map<out K, V>.mapKeys(transform: (Map.Entry<K, V>) -> R): Map<R, V>
6. fun <K, V, R> Map<out K, V>.mapKeys(transform: (Map.Entry<K, V>) -> R): Map<R, V>
7. fun <K, V, R> Map<out K, V>.mapValues(transform: (Map.Entry<K, V>) -> R): Map<K, R>
8. fun <K, V, R, M : MutableMap<in K, in R>> Map<out K, V>.mapValuesTo(destination: M, transform: (Map.Entry<K, V>) -> R): M	

Now practicing ->

0. Initial data for practicing


data class Address(
	val street: String,
	val postcode: String
)
 
data class Customer(
	val firstName: String,
	val lastName: String,
	val age: Int
)

data class Person(
	val fullname: String,
	val age: Int,
	val address: Address
)

val customerMap = mapOf(Pair(Customer("Jack", "Davis", 25), Address("NANTERRE CT", "77471")),
						Pair(Customer("Mary", "Taylor", 37), Address("W NORMA ST", "77009")),
						Pair(Customer("Peter", "Thomas",17), Address("S NUGENT AVE", "77571")),
						Pair(Customer("Amos", "Nelson",23), Address("E NAVAHO TRL", "77449")),
						Pair(Customer("Craig", "White",45), Address("AVE N", "77587")),
						Pair(Customer("Laura", "Lewis", 32), Address("NANTERRE CT", "77471")),
						Pair(Customer("Steven", "Harris", 39), Address("S NUGENT AVE", "77571")),
						Pair(Customer("Paul", "Moore", 18), Address("E NAVAHO TRL", "77449")),
						Pair(Customer("Mary", "Cook", 61), Address("S NUGENT AVE", "77571")),
						Pair(null, null))

1. Transform a given Kotlin Map to List

1.1 map()

Method signature:

public inline fun <K, V, R> Map<out K, V>.map(transform: (Map.Entry<K, V>) -> R): List<R>

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

Practice:


var personList = customerMap.map {
									(customer, address) ->
											if(customer != null && address != null)
												Person(customer.firstName + " " + customer.lastName, customer.age, address)
											else null
								 }

personList.forEach{ println(it) }
/*
	Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
	Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
	Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
	Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
	Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
	Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
	Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
	Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
	Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
	null
*/

1.2 mapNotNull()

Method signature:

public inline fun <K, V, R : Any> Map<out K, V>.mapNotNull(transform: (Map.Entry<K, V>) -> R?): List<R>

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

Practice:


personList = customerMap.mapNotNull{
							(customer, address) ->
									if(customer != null && address != null)
										Person(customer.firstName + " " + customer.lastName, customer.age, address)
									else null
					  }

personList.forEach{ println(it) }
/*
	Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
	Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
	Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
	Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
	Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
	Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
	Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
	Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
	Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
*/

1.3 mapTo()

Method signature:

public inline fun <K, V, R, C : MutableCollection<in R>> Map<out K, V>.mapTo(destination: C, transform: (Map.Entry<K, V>) -> R): C

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

Practice:


var mutablePersonList = mutableListOf()
customerMap.mapTo(mutablePersonList, {
							(customer, address) ->
									if(customer != null && address != null)
										Person(customer.firstName + " " + customer.lastName, customer.age, address)
									else null
					  }
				 )

mutablePersonList.forEach{ println(it) }
/*
	Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
	Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
	Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
	Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
	Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
	Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
	Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
	Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
	Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
	null
*/

1.4 mapNotNullTo()

Method signature:

public inline fun <K, V, R : Any, C : MutableCollection<in R>> Map<out K, V>.mapNotNullTo(destination: C, transform: (Map.Entry<K, V>) -> R?): C

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

Practice:


mutablePersonList.clear()
customerMap.mapNotNullTo(mutablePersonList, {
							(customer, address) ->
								if(customer != null && address != null)
									Person(customer.firstName + " " + customer.lastName, customer.age, address)
								else null
					  }
				 )

mutablePersonList.forEach{ println(it) }
/*
	Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
	Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
	Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
	Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
	Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
	Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
	Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
	Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
	Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
*/

2. Transform a given Kotlin Map to new Map

2.1 mapKeys()

Method signature:

public inline fun <K, V, R> Map<out K, V>.mapKeys(transform: (Map.Entry<K, V>) -> R): Map<R, V>

-> 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:


val newCustomerMap = customerMap.mapKeys{
											(customer, _) ->
												if(customer!=null)
													customer.firstName + " " + customer.lastName
												else null
}

newCustomerMap.forEach{ println(it) }
/*
	Jack Davis=Address(street=NANTERRE CT, postcode=77471)
	Mary Taylor=Address(street=W NORMA ST, postcode=77009)
	Peter Thomas=Address(street=S NUGENT AVE, postcode=77571)
	Amos Nelson=Address(street=E NAVAHO TRL, postcode=77449)
	Craig White=Address(street=AVE N, postcode=77587)
	Laura Lewis=Address(street=NANTERRE CT, postcode=77471)
	Steven Harris=Address(street=S NUGENT AVE, postcode=77571)
	Paul Moore=Address(street=E NAVAHO TRL, postcode=77449)
	Mary Cook=Address(street=S NUGENT AVE, postcode=77571)
	null=null
*/

2.2 mapKeysTo()

Method signature:

public inline fun <K, V, R, M : MutableMap<in R, in V>> Map<out K, V>.mapKeysTo(destination: M, transform: (Map.Entry<K, V>) -> R): M

-> 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:


var newMutableCustomerMap = mutableMapOf()
customerMap.mapKeysTo(newMutableCustomerMap, {	
												(customer, _) ->
														if(customer!=null)
															customer.firstName + " " + customer.lastName
														else null
					})	

newMutableCustomerMap.forEach{ println(it) }
/*
	Jack Davis=Address(street=NANTERRE CT, postcode=77471)
	Mary Taylor=Address(street=W NORMA ST, postcode=77009)
	Peter Thomas=Address(street=S NUGENT AVE, postcode=77571)
	Amos Nelson=Address(street=E NAVAHO TRL, postcode=77449)
	Craig White=Address(street=AVE N, postcode=77587)
	Laura Lewis=Address(street=NANTERRE CT, postcode=77471)
	Steven Harris=Address(street=S NUGENT AVE, postcode=77571)
	Paul Moore=Address(street=E NAVAHO TRL, postcode=77449)
	Mary Cook=Address(street=S NUGENT AVE, postcode=77571)
	null=null
*/

2.3 mapValues()

Method signature:

public inline fun <K, V, R> Map<out K, V>.mapValues(transform: (Map.Entry<K, V>) -> R): Map<K, R>

-> 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:


val newCustomerPersonMap = customerMap.mapValues {
											(customer, address) ->
												if(customer != null && address != null)
													Person(customer.firstName + " " + customer.lastName, customer.age, address)
												else null
					       }

newCustomerPersonMap.forEach{ println(it) }
/*
	Customer(firstName=Jack, lastName=Davis, age=25)=Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
	Customer(firstName=Mary, lastName=Taylor, age=37)=Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
	Customer(firstName=Peter, lastName=Thomas, age=17)=Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
	Customer(firstName=Amos, lastName=Nelson, age=23)=Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
	Customer(firstName=Craig, lastName=White, age=45)=Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
	Customer(firstName=Laura, lastName=Lewis, age=32)=Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
	Customer(firstName=Steven, lastName=Harris, age=39)=Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
	Customer(firstName=Paul, lastName=Moore, age=18)=Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
	Customer(firstName=Mary, lastName=Cook, age=61)=Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
	null=null
*/

2.4 mapValuesTo()

Method signature:

public inline fun <K, V, R, M : MutableMap<in K, in R>> Map<out K, V>.mapValuesTo(destination: M, transform: (Map.Entry<K, V>) -> R): M

-> 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:


var newMutableCustomerPersonMap = mutableMapOf()
customerMap.mapValuesTo (newMutableCustomerPersonMap, {
											(customer, address) ->
												if(customer != null && address != null)
													Person(customer.firstName + " " + customer.lastName, customer.age, address)
												else null
					       })

newCustomerPersonMap.forEach{ println(it) }
/*
	Customer(firstName=Jack, lastName=Davis, age=25)=Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
	Customer(firstName=Mary, lastName=Taylor, age=37)=Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
	Customer(firstName=Peter, lastName=Thomas, age=17)=Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
	Customer(firstName=Amos, lastName=Nelson, age=23)=Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
	Customer(firstName=Craig, lastName=White, age=45)=Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
	Customer(firstName=Laura, lastName=Lewis, age=32)=Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
	Customer(firstName=Steven, lastName=Harris, age=39)=Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
	Customer(firstName=Paul, lastName=Moore, age=18)=Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
	Customer(firstName=Mary, lastName=Cook, age=61)=Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
	null=null
*/

II. Full sourcecode


package com.javasampleapproach.kotlin.map

data class Address(
	val street: String,
	val postcode: String
)
 
data class Customer(
	val firstName: String,
	val lastName: String,
	val age: Int
)

data class Person(
	val fullname: String,
	val age: Int,
	val address: Address
)

fun main(args : Array){
	/*
		1. fun  Map.map(transform: (Map.Entry) -> R): List
		2. fun  Map.mapNotNull(transform: (Map.Entry) -> R?): List
		3. fun > Map.mapTo(destination: C, transform: (Map.Entry) -> R): C
		4. fun > Map.mapNotNullTo(destination: C, transform: (Map.Entry) -> R?): C
		5. fun  Map.mapKeys(transform: (Map.Entry) -> R): Map
		6. fun > Map.mapKeysTo(destination: M, transform: (Map.Entry) -> R): M
		7. fun  Map.mapValues(transform: (Map.Entry) -> R): Map
		8. fun > Map.mapValuesTo(destination: M, transform: (Map.Entry) -> R): M
 	*/
	 
	val customerMap = mapOf(Pair(Customer("Jack", "Davis", 25), Address("NANTERRE CT", "77471")),
							Pair(Customer("Mary", "Taylor", 37), Address("W NORMA ST", "77009")),
							Pair(Customer("Peter", "Thomas",17), Address("S NUGENT AVE", "77571")),
							Pair(Customer("Amos", "Nelson",23), Address("E NAVAHO TRL", "77449")),
							Pair(Customer("Craig", "White",45), Address("AVE N", "77587")),
							Pair(Customer("Laura", "Lewis", 32), Address("NANTERRE CT", "77471")),
							Pair(Customer("Steven", "Harris", 39), Address("S NUGENT AVE", "77571")),
							Pair(Customer("Paul", "Moore", 18), Address("E NAVAHO TRL", "77449")),
							Pair(Customer("Mary", "Cook", 61), Address("S NUGENT AVE", "77571")),
							Pair(null, null))

	println("-------------------------------1-----------------------------------")
	
	//
 	//	1. public inline fun  Map.map(transform: (Map.Entry) -> R): List
 	//
	var personList = customerMap.map {
										(customer, address) ->
												if(customer != null && address != null)
													Person(customer.firstName + " " + customer.lastName, customer.age, address)
												else null
									 }
	
	personList.forEach{ println(it) }
	/*
		Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
		Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
		Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
		Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
		Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
		Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
		Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
		Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
		Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
		null
	*/

	//
 	//	2. public inline fun  Map.mapNotNull(transform: (Map.Entry) -> R?): List
 	//
	println("-------------------------------2-----------------------------------")
	personList = customerMap.mapNotNull{
								(customer, address) ->
										if(customer != null && address != null)
											Person(customer.firstName + " " + customer.lastName, customer.age, address)
										else null
						  }
	
	personList.forEach{ println(it) }
	/*
		Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
		Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
		Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
		Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
		Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
		Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
		Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
		Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
		Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
	*/
	
	//
	// 3. public inline fun > Map.mapTo(destination: C, transform: (Map.Entry) -> R): C
	//
	println("-------------------------------3-----------------------------------")
	var mutablePersonList = mutableListOf()
	customerMap.mapTo(mutablePersonList, {
								(customer, address) ->
										if(customer != null && address != null)
											Person(customer.firstName + " " + customer.lastName, customer.age, address)
										else null
						  }
					 )
	
	mutablePersonList.forEach{ println(it) }
	/*
		Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
		Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
		Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
		Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
		Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
		Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
		Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
		Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
		Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
		null
	*/
	
	
	
	
	//
	// 4. public inline fun > Map.mapNotNullTo(destination: C, transform: (Map.Entry) -> R?): C
	//
	println("-------------------------------4-----------------------------------")
	mutablePersonList.clear()
	customerMap.mapNotNullTo(mutablePersonList, {
								(customer, address) ->
									if(customer != null && address != null)
										Person(customer.firstName + " " + customer.lastName, customer.age, address)
									else null
						  }
					 )
	
	mutablePersonList.forEach{ println(it) }
	/*
		Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
		Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
		Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
		Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
		Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
		Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
		Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
		Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
		Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
	*/
	
	//
	// 5. public inline fun  Map.mapKeys(transform: (Map.Entry) -> R): Map
	//
	println("-------------------------------5-----------------------------------")
	val newCustomerMap = customerMap.mapKeys{
												(customer, _) ->
													if(customer!=null)
														customer.firstName + " " + customer.lastName
													else null
	}
	
	newCustomerMap.forEach{ println(it) }
	/*
		Jack Davis=Address(street=NANTERRE CT, postcode=77471)
		Mary Taylor=Address(street=W NORMA ST, postcode=77009)
		Peter Thomas=Address(street=S NUGENT AVE, postcode=77571)
		Amos Nelson=Address(street=E NAVAHO TRL, postcode=77449)
		Craig White=Address(street=AVE N, postcode=77587)
		Laura Lewis=Address(street=NANTERRE CT, postcode=77471)
		Steven Harris=Address(street=S NUGENT AVE, postcode=77571)
		Paul Moore=Address(street=E NAVAHO TRL, postcode=77449)
		Mary Cook=Address(street=S NUGENT AVE, postcode=77571)
		null=null
 	*/

	//
	// 6. public inline fun > Map.mapKeysTo(destination: M, transform: (Map.Entry) -> R): M
	//
	println("-------------------------------6-----------------------------------")
	var newMutableCustomerMap = mutableMapOf()
	customerMap.mapKeysTo(newMutableCustomerMap, {	
													(customer, _) ->
															if(customer!=null)
																customer.firstName + " " + customer.lastName
															else null
						})	
	
	newMutableCustomerMap.forEach{ println(it) }
	/*
		Jack Davis=Address(street=NANTERRE CT, postcode=77471)
		Mary Taylor=Address(street=W NORMA ST, postcode=77009)
		Peter Thomas=Address(street=S NUGENT AVE, postcode=77571)
		Amos Nelson=Address(street=E NAVAHO TRL, postcode=77449)
		Craig White=Address(street=AVE N, postcode=77587)
		Laura Lewis=Address(street=NANTERRE CT, postcode=77471)
		Steven Harris=Address(street=S NUGENT AVE, postcode=77571)
		Paul Moore=Address(street=E NAVAHO TRL, postcode=77449)
		Mary Cook=Address(street=S NUGENT AVE, postcode=77571)
		null=null
 	*/
	
	//
	// 7. public inline fun  Map.mapValues(transform: (Map.Entry) -> R): Map 
	//
	println("-------------------------------7-----------------------------------")
	val newCustomerPersonMap = customerMap.mapValues {
												(customer, address) ->
													if(customer != null && address != null)
														Person(customer.firstName + " " + customer.lastName, customer.age, address)
													else null
						       }
	
	newCustomerPersonMap.forEach{ println(it) }
	/*
		Customer(firstName=Jack, lastName=Davis, age=25)=Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
		Customer(firstName=Mary, lastName=Taylor, age=37)=Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
		Customer(firstName=Peter, lastName=Thomas, age=17)=Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
		Customer(firstName=Amos, lastName=Nelson, age=23)=Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
		Customer(firstName=Craig, lastName=White, age=45)=Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
		Customer(firstName=Laura, lastName=Lewis, age=32)=Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
		Customer(firstName=Steven, lastName=Harris, age=39)=Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
		Customer(firstName=Paul, lastName=Moore, age=18)=Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
		Customer(firstName=Mary, lastName=Cook, age=61)=Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
		null=null
 	*/
	
	//
	// 8. fun > Map.mapValuesTo(destination: M, transform: (Map.Entry) -> R): M
	//
	println("-------------------------------8-----------------------------------")
	var newMutableCustomerPersonMap = mutableMapOf()
	customerMap.mapValuesTo (newMutableCustomerPersonMap, {
												(customer, address) ->
													if(customer != null && address != null)
														Person(customer.firstName + " " + customer.lastName, customer.age, address)
													else null
						       })
	
	newCustomerPersonMap.forEach{ println(it) }
	/*
		Customer(firstName=Jack, lastName=Davis, age=25)=Person(fullname=Jack Davis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
		Customer(firstName=Mary, lastName=Taylor, age=37)=Person(fullname=Mary Taylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
		Customer(firstName=Peter, lastName=Thomas, age=17)=Person(fullname=Peter Thomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
		Customer(firstName=Amos, lastName=Nelson, age=23)=Person(fullname=Amos Nelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
		Customer(firstName=Craig, lastName=White, age=45)=Person(fullname=Craig White, age=45, address=Address(street=AVE N, postcode=77587))
		Customer(firstName=Laura, lastName=Lewis, age=32)=Person(fullname=Laura Lewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
		Customer(firstName=Steven, lastName=Harris, age=39)=Person(fullname=Steven Harris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
		Customer(firstName=Paul, lastName=Moore, age=18)=Person(fullname=Paul Moore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
		Customer(firstName=Mary, lastName=Cook, age=61)=Person(fullname=Mary Cook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
		null=null
 	*/
}


By grokonez | January 24, 2018.

Last updated on May 7, 2021.



Related Posts


Got Something To Say:

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

*