DEV Community

loizenai
loizenai

Posted on

Convert Kotlin Map to List

https://grokonez.com/kotlin/kotlin-tutorial-convert-kotlin-map-list

Convert Kotlin Map to List

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

Related posts:

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:

More at:

https://grokonez.com/kotlin/kotlin-tutorial-convert-kotlin-map-list

Convert Kotlin Map to List

Top comments (0)