Kotlin Map to List Example

Kotlin Map to List Example

In the tutorial, I show how to convert Kotlin Map to List with detail steps and clearly examples.

Convert Kotlin Map to List with 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]

Convert Map to List in Kotlin with Map Object

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

val custStores = mutableMapOf<Long, Customer>()
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<Address> = mutableListOf<Address>();
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)]

Create Kotlin Model

– 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()) {
}

Kotlin Program Map to List

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)] }

– Reference: Kotlin Collection Transformations

– Related posts:

Leave a Reply

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