W3cubDocs

/Kotlin

mapKeys

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

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.

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

fun main(args: Array<String>) {
//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
}

© 2010–2018 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map-keys.html