Convert Kotlin Map to List

kotlin-tutorial-convert-kotlin-map-list-feature-image

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

Related posts:
Convert Kotlin List to Map

I. Convert Kotlin Map to List

1. Basic Method

– Use public fun Map.toList(): List> to returns a [List] containing all key-value pairs


val simpleMap = hashMapOf("foo" to 1, "bar" to 2)
val pairKeyValueList = simpleMap.toList()
println(pairKeyValueList) // [(bar, 2), (foo, 1)]

– Use public Collection values() to return a view of the values:


val valueList = simpleMap.values
println(valueList) // [2, 1]

– Use public Set keySet() to return a set view of the keys:


val keyList = simpleMap.keys
println(keyList) // [bar, foo]

2. Map Object

– When working with Map Object, we can associate with map(transform: (T) -> R) function to customize a returned-list:


val custStores = mutableMapOf()
custStores.put(1, Customer("Jack", 20, Address("NANTERRE CT", "77471")))
custStores.put(2, Customer("Peter", 25, Address("W NORMA ST", "77009")))

val addressList = custStores.values.map{ x -> x.address }
println(addressList) // [Address(street=NANTERRE CT, postcode=77471), Address(street=W NORMA ST, postcode=77009)]

– Use MutableList interface to modifiable list:


var mutableAddressList: MutableList
= mutableListOf
(); mutableAddressList.addAll(custStores.values.map{ x -> x.address }) mutableAddressList.add(Address("E NAVAHO TRL", "77449")); println(mutableAddressList) // [Address(street=NANTERRE CT, postcode=77471), Address(street=W NORMA ST, postcode=77009), Address(street=E NAVAHO TRL, postcode=77449)]

II. Practice

1. Create model data

– Create an Address model:


data class Address(
	var street : String? = null,
	var postcode : String? = null
){}

– Create an Customer model:


data class Customer(
	var name: String? = null,
	var age: Int? = null,
	var address: Address = Address()) {
}

2. Kotlin program


fun main(args : Array) {
	//
	// 1. Work with basic Map
	//
	val simpleMap = hashMapOf("foo" to 1, "bar" to 2)
	println(simpleMap) // {bar=2, foo=1}
	
	// 1.1 a List with pair Key-Value 
	val pairKeyValueList = simpleMap.toList()
	println(pairKeyValueList) // [(bar, 2), (foo, 1)]
	
	// 1.2 a List with values
	val valueList = simpleMap.values
	println(valueList) // [2, 1]
	
	// 1.3 a List with keys 
	val keyList = simpleMap.keys
	println(keyList) // [bar, foo]
	
	//
	// 2. Work with Object Map
	//
	val custStores = mutableMapOf()
	custStores.put(1, Customer("Jack", 20, Address("NANTERRE CT", "77471")))
	custStores.put(2, Customer("Peter", 25, Address("W NORMA ST", "77009")))
	println(custStores); // {1=Customer(name=Jack, age=20, address=Address(street=NANTERRE CT, postcode=77471)), 2=Customer(name=Peter, age=25, address=Address(street=W NORMA ST, postcode=77009))}
	
	// 2.1 a List with pair Key-Value Objects
	val pairKeyValueCustList = custStores.toList();
	println(pairKeyValueCustList) // [(1, Customer(name=Jack, age=20, address=Address(street=NANTERRE CT, postcode=77471))), (2, Customer(name=Peter, age=25, address=Address(street=W NORMA ST, postcode=77009)))]
	
	// 2.2 a List with Object Values
	val customersList = custStores.values
	println(customersList) // [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))]
	
	// 2.3 Customize Objects with map function: (Customer -> Address)
	val addressList = custStores.values.map{ x -> x.address }
	println(addressList) // [Address(street=NANTERRE CT, postcode=77471), Address(street=W NORMA ST, postcode=77009)]
	
	// 2.4 Using MutableList interface for a modifiable List
	var mutableAddressList: MutableList
= mutableListOf
(); mutableAddressList.addAll(custStores.values.map{ x -> x.address }) mutableAddressList.add(Address("E NAVAHO TRL", "77449")); println(mutableAddressList) // [Address(street=NANTERRE CT, postcode=77471), Address(street=W NORMA ST, postcode=77009), Address(street=E NAVAHO TRL, postcode=77449)] }

III. Sourcecode

KotlinMapToList



By grokonez | December 11, 2017.

Last updated on April 17, 2021.



Related Posts


Got Something To Say:

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

*