Sobes.tech
Junior — Middle

How to convert a key-value pair collection into a list of numeric values in Kotlin?

sobes.tech AI

Answer from AI

In Kotlin, to convert a collection of key-value pairs (for example, a Map) into a list of numeric values, you can use the map function specifying that you want to take only the values and convert them to a numeric type.

Example:

val map = mapOf("a" to "1", "b" to "2", "c" to "3")

// Convert values to Int and collect into a list
val numbers: List<Int> = map.values.map { it.toInt() }

println(numbers) // Outputs: [1, 2, 3]

If the values are already numeric, simply use map.values.toList().