How can I flatten HashMaps and get only a list of keys in Kotlin?

Viewed 39

I need to get only a list of keys from a hash map.

Here is how I do it:

data is hashmap type.

val flatten = data.flatMap { it.entries }
val keys = flatten.map { it.key }

As you can see on the rows above first, the data is flattened, and then with help of the map method I create a list only of keys. But it looks like that flatten.map { it.key } work slow.

My question is, is there any other way to fetch a list of keys from Hashmap, or how can I improve the performance of the code above?

0 Answers
Related