W3cubDocs

/Kotlin

mapKeys

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
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–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map-keys.html