In the tutorial, JavaSampleApproach will show you how to convert Kotlin List to Map.
Related posts:
– Convert Kotlin Map to List
Contents
A. Convert Kotlin List to Map
Initialize data:
1 2 3 |
val custList = listOf<Customer>(Customer("Jack", 20, Address("NANTERRE CT", "77471")), Customer("Peter", 25, Address("W NORMA ST", "77009"))) println("custList = $custList") // custList = [Customer(name=Jack, age=20, address=Address(street=NANTERRE CT, postcode=77471)), Customer(name=Peter, age=25, address=Address(street=W NORMA ST, postcode=77009))] |
I. Use toMap() method
Method signature: public fun
-> Returns a new map containing all key-value pairs from the given collection.
1. Create a read-only map
1 2 |
val toMap = custList.map { it.name to it.address }.toMap() println("toMap = $toMap") // toMap = {Jack=Address(street=NANTERRE CT, postcode=77471), Peter=Address(street=W NORMA ST, postcode=77009)} |
2. Create a modifiable map
1 2 3 4 |
var modifiableMap = mutableMapOf<String?, Address>() modifiableMap.putAll(custList.map { it.name to it.address }.toMap()) modifiableMap.put("Mary", Address("E NAVAHO TRL", "77449")) println("modifiableMap = $modifiableMap") // modifiableMap = {Jack=Address(street=NANTERRE CT, postcode=77471), Peter=Address(street=W NORMA ST, postcode=77009), Mary=Address(street=E NAVAHO TRL, postcode=77449)} |
II. Use associate* methods
1. associate()
Method signature: public inline fun
1 2 |
val associateMap = custList.associate { it.name to it.address.street } println("associateMap = $associateMap") // associateMap = {Jack=NANTERRE CT, Peter=W NORMA ST} |
2. associateBy()
public inline fun Iterable.associateBy(keySelector: (T) -> K): Map
1 2 |
val associateByMap = custList.associateBy { it.name } println("associateByMap = $associateByMap") // associateByMap = {Jack=Customer(name=Jack, age=20, address=Address(street=NANTERRE CT, postcode=77471)), Peter=Customer(name=Peter, age=25, address=Address(street=W NORMA ST, postcode=77009))} |
public inline fun Iterable.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map
1 2 |
val associateByMap2 = custList.associateBy({it.name}, {it.address}) println("associateByMap2 = $associateByMap2") // associateByMap2 = {Jack=Address(street=NANTERRE CT, postcode=77471), Peter=Address(street=W NORMA ST, postcode=77009)} |
3. associateTo()
Method signature: public inline fun
1 2 3 |
var associateToMap = mutableMapOf<String?, Address>() custList.associateTo(associateToMap, {it.name to it.address}) println("associateToMap = $associateToMap") // associateToMap = {Jack=Address(street=NANTERRE CT, postcode=77471), Peter=Address(street=W NORMA ST, postcode=77009)} |
4. associateByTo()
public inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K): M
1 2 3 |
var associateByToMap = mutableMapOf<String?, Customer>() custList.associateByTo(associateByToMap, {it.name}) println("associateByToMap = $associateByToMap") // associateByToMap = {Jack=Customer(name=Jack, age=20, address=Address(street=NANTERRE CT, postcode=77471)), Peter=Customer(name=Peter, age=25, address=Address(street=W NORMA ST, postcode=77009))} |
public inline fun > Iterable.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M
1 2 3 |
var associateByToMap2 = mutableMapOf<String?, Address>() custList.associateByTo(associateByToMap2, {it.name}, {it.address}) println("associateByToMap2 = $associateByToMap2") // associateByToMap2 = {Jack=Address(street=NANTERRE CT, postcode=77471), Peter=Address(street=W NORMA ST, postcode=77009)} |
B. Practice
I. Implement
We create a Kotlin project as below:
1. Create data models
– Create Address model:
1 2 3 4 5 6 |
package com.javasampleapproach.list2map.model data class Address( var street : String? = null, var postcode : String? = null ){} |
– Create Customer model:
1 2 3 4 5 6 7 |
package com.javasampleapproach.list2map.model data class Customer( var name: String? = null, var age: Int? = null, var address: Address = Address()) { } |
2. Kotlin Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.javasampleapproach.list2map import com.javasampleapproach.list2map.model.Address import com.javasampleapproach.list2map.model.Customer fun main(args : Array<String>) { // initial list of Customers val custList = listOf<Customer>(Customer("Jack", 20, Address("NANTERRE CT", "77471")), Customer("Peter", 25, Address("W NORMA ST", "77009"))) println("custList = $custList") // custList = [Customer(name=Jack, age=20, address=Address(street=NANTERRE CT, postcode=77471)), Customer(name=Peter, age=25, address=Address(street=W NORMA ST, postcode=77009))] /* * I. use toMap() function to convert List -> Map */ // 1. create a read-only map val toMap = custList.map { it.name to it.address }.toMap() println("toMap = $toMap") // toMap = {Jack=Address(street=NANTERRE CT, postcode=77471), Peter=Address(street=W NORMA ST, postcode=77009)} // 2. create a modifiable map var modifiableMap = mutableMapOf<String?, Address>() modifiableMap.putAll(custList.map { it.name to it.address }.toMap()) modifiableMap.put("Mary", Address("E NAVAHO TRL", "77449")) println("modifiableMap = $modifiableMap") // modifiableMap = {Jack=Address(street=NANTERRE CT, postcode=77471), Peter=Address(street=W NORMA ST, postcode=77009), Mary=Address(street=E NAVAHO TRL, postcode=77449)} /* * II. use associate* methods */ // 1. use associate() // -> public inline fun <T, K, V> Iterable<T>.associate(transform: (T) -> Pair<K, V>): Map<K, V> val associateMap = custList.associate { it.name to it.address.street } println("associateMap = $associateMap") // associateMap = {Jack=NANTERRE CT, Peter=W NORMA ST} // 2. use associateBy // -> 2.1 use public inline fun <T, K> Iterable<T>.associateBy(keySelector: (T) -> K): Map<K, T> val associateByMap = custList.associateBy { it.name } println("associateByMap = $associateByMap") // associateByMap = {Jack=Customer(name=Jack, age=20, address=Address(street=NANTERRE CT, postcode=77471)), Peter=Customer(name=Peter, age=25, address=Address(street=W NORMA ST, postcode=77009))} // -> 2.2 use public inline fun <T, K, V> Iterable<T>.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, V> val associateByMap2 = custList.associateBy({it.name}, {it.address}) println("associateByMap2 = $associateByMap2") // associateByMap2 = {Jack=Address(street=NANTERRE CT, postcode=77471), Peter=Address(street=W NORMA ST, postcode=77009)} // 3. use associateTo // -> public inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateTo(destination: M, transform: (T) -> Pair<K, V>): M var associateToMap = mutableMapOf<String?, Address>() custList.associateTo(associateToMap, {it.name to it.address}) println("associateToMap = $associateToMap") // associateToMap = {Jack=Address(street=NANTERRE CT, postcode=77471), Peter=Address(street=W NORMA ST, postcode=77009)} // 4. use associateByTo // ->4.1 use public inline fun <T, K, M : MutableMap<in K, in T>> Iterable<T>.associateByTo(destination: M, keySelector: (T) -> K): M var associateByToMap = mutableMapOf<String?, Customer>() custList.associateByTo(associateByToMap, {it.name}) println("associateByToMap = $associateByToMap") // associateByToMap = {Jack=Customer(name=Jack, age=20, address=Address(street=NANTERRE CT, postcode=77471)), Peter=Customer(name=Peter, age=25, address=Address(street=W NORMA ST, postcode=77009))} // -> 4.2 use public inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M var associateByToMap2 = mutableMapOf<String?, Address>() custList.associateByTo(associateByToMap2, {it.name}, {it.address}) println("associateByToMap2 = $associateByToMap2") // associateByToMap2 = {Jack=Address(street=NANTERRE CT, postcode=77471), Peter=Address(street=W NORMA ST, postcode=77009)} } |
II. Sourcecode
Last updated on September 12, 2018.