Solution to the Reversing the Values in a Map Challenge

fun flipValues(values: Map<String, Double>): Map<Double, String> {
    return { it.value to it.key }.toMap()

brillant solution! Thanks!

Slick! I was trying to figure out the syntax for this and couldn’t. Nicely done. Mine wound up looking a bit more clunky :roll_eyes:

fun flipValues(map: Map<String, Double>): Map<Double, String>{
val studentGrades = map.values.toList()
val studentName = map.keys.toList()

Just adding my spin to this solution using extensions and generics.

fun <K, V> Map<K, V>.flipValues(): Map<V, K>{
return {
it.value to it.key

edit: I replied to the wrong post and only now I notice this solution being very similar to mine. Oh well.

I added some generics to my solution:

fun <T1,T2> flipValues(m: Map<T1, T2>) : Map<T2, T1> {
return { (key, value) -> value to key }.toMap()