将地图转换为Kotlin中的地图列表

我正试图转换一个传统的地图:

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

到一个像这样的固定键的地图列表:

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

现在我有一个不好看的解决方案,并没有真正利用Kotlin的功能特性。 我想知道是否有更清晰的解决方案:

 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 } 

只要使用Map#map就足够了,例如:

 fun toListOfMaps(map: Map<Int, String>): List<Map<String, Any>> { // v--- destructuring `Map.Entry` here return map.map { (number, answer) -> mapOf("number" to number, "answer" to answer) } }