Convert HashMap to List in Kotlin
In Kotlin, HashMap has 2 properties,keys
& values
each of these properties will return a list. See below example to return keys as list and values as listimport java.util.HashMap
fun main(array: Array<String>) {
var map = HashMap<Int, String>();
map.put(10, "Ten")
map.put(20, "Twenty")
map.put(30, "Thirty")
map.put(40, "Fourty")
map.put(50, "Fifty")
var keysList = ArrayList(map.keys);
var valuesList = ArrayList(map.values);
println("Keys list : $keysList")
println("Values list : $valuesList")
}
Output
Keys list : [50, 20, 40, 10, 30]
Values list : [Fifty, Twenty, Fourty, Ten, Thirty]
Values list : [Fifty, Twenty, Fourty, Ten, Thirty]