English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this program, you will learn different methods to convert a map to a list in Kotlin.
import java.util.ArrayList import java.util.HashMap fun main(args: Array<String>) { val map = HashMap<Int, String>() map.put(1, "a") map.put(2, "b") map.put(3, "c") map.put(4, "d") map.put(5, "e") val keyList = ArrayList(map.keys) val valueList = ArrayList(map.values) println("Key List: $keyList") println("Value List: $valueList") }
When running the program, the output is:
Key List: [1, 2, 3, 4, 5] Value List: [a, b, c, d, e]
In the above program, we have a named integer and string map. Since the map contains a key-value pair, we need two lists to store them, namely keyList and valueList.
We use the map's keySet() method to get all keys and create an ArrayList key list from them. Similarly, we use the map's values() method to get all values and create an ArrayList valueList.
The following is the equivalent Java code:Java Program to Convert Map to List.