mapKeys

inline fun <K, V, R> Map<out K, V>.mapKeys(transform: (Map.Entry<K, V>) -> R): Map<R, V>(source)

Returns a new Map with entries having the keys obtained by applying the transform function to each entry in this Map and the values of this map.

In case if any two entries are mapped to the equal keys, the value of the latter one will overwrite the value associated with the former one.

The returned map preserves the entry iteration order of the original map.

Since Kotlin

1.1

Samples

import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val map1 = mapOf("beer" to 2.7, "bisquit" to 5.8)
val map2 = map1.mapKeys { it.key.length }
println(map2) // {4=2.7, 7=5.8}

val map3 = map1.mapKeys { it.key.take(1) }
println(map3) // {b=5.8} 
   //sampleEnd
}
inline fun <K, V, R> Map<out K, V>.mapKeys(transform: (Map.Entry<K, V>) -> R): Map<R, V>(source)

Returns a new Map with entries having the keys obtained by applying the transform function to each entry in this Map and the values of this map.

In case if any two entries are mapped to the equal keys, the value of the latter one will overwrite the value associated with the former one.

The returned map preserves the entry iteration order of the original map.

Since Kotlin

1.0

Samples

import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val map1 = mapOf("beer" to 2.7, "bisquit" to 5.8)
val map2 = map1.mapKeys { it.key.length }
println(map2) // {4=2.7, 7=5.8}

val map3 = map1.mapKeys { it.key.take(1) }
println(map3) // {b=5.8} 
   //sampleEnd
}