Convert Kotlin List to Map

convert-kotlin-list-map-feature-image

In the tutorial, JavaSampleApproach will show you how to convert Kotlin List to Map.

Related posts:
Convert Kotlin Map to List

A. Convert Kotlin List to Map

Initialize data:

val custList = listOf(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 Iterable>.toMap(): Map
-> Returns a new map containing all key-value pairs from the given collection.

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()
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 Iterable.associate(transform: (T) -> Pair): Map

val associateMap = custList.associate { it.name to it.address.street }
println("associateMap = $associateMap") // associateMap = {Jack=NANTERRE CT, Peter=W NORMA ST}
2. associateBy()
  • use public inline fun Iterable.associateBy(keySelector: (T) -> K): Map
  • 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))}
    
  • use public inline fun Iterable.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map
  • 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> Iterable.associateTo(destination: M, transform: (T) -> Pair): M

    var associateToMap = mutableMapOf()
    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()
  • use public inline fun> Iterable.associateByTo(destination: M, keySelector: (T) -> K): M
  • var associateByToMap = mutableMapOf() 
    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))}
    
  • use public inline fun> Iterable.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M
  • var associateByToMap2 = mutableMapOf() 
    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:

    kotlin list to map - project structure

    1. Create data models

    – Create Address model:

    package com.javasampleapproach.list2map.model
    
    data class Address(
    	var street : String? = null,
    	var postcode : String? = null
    ){}
    

    – Create Customer model:

    package com.javasampleapproach.list2map.model
    
    data class Customer(
    	var name: String? = null,
    	var age: Int? = null,
    	var address: Address = Address()) {
    }
    
    2. Kotlin Program
    package com.javasampleapproach.list2map
    
    import com.javasampleapproach.list2map.model.Address
    import com.javasampleapproach.list2map.model.Customer
    
    fun main(args : Array) {
    	// initial list of Customers
    	val custList = listOf(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()
    	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  Iterable.associate(transform: (T) -> Pair): Map
    	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  Iterable.associateBy(keySelector: (T) -> K): Map 
    	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  Iterable.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map
    	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 > Iterable.associateTo(destination: M, transform: (T) -> Pair): M
    	var associateToMap = mutableMapOf()
    	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 > Iterable.associateByTo(destination: M, keySelector: (T) -> K): M
    	var associateByToMap = mutableMapOf() 
    	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 > Iterable.associateByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M
    	var associateByToMap2 = mutableMapOf() 
    	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

    KotlinConvertListToMap



    By grokonez | December 12, 2017.

    Last updated on February 6, 2020.



    Related Posts


    Got Something To Say:

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

    *