Convert maps to list of maps in Kotlin

Viewed 2497

I am trying to convert a traditional map:

1 -> "YES",
2 -> "NO",
3 -> "YES",
...

To a list of maps with fixed keys like such:

[
  <number -> 1,
   answer -> "YES">,
  <number -> 2,
   answer -> "NO">,
  ...
]

Right now I have a solution which does not look good and is not really making use of Kotlin's functional features. I was wondering if there was a clearer solution that did:

fun toListOfMaps(map: Map<Int, String>): List<Map<String, Any>> {
    val listOfMaps = emptyList<Map<String, Any>>().toMutableList()

    for (entry in map) {
        val mapElement = mapOf(
                "number" to entry.component1(),
                "answer" to entry.component2()
        )
        listOfMaps.add(mapElement)
    }

    return listOfMaps
}
1 Answers
Related