Idiomatic way to transform map in kotlin?

I don't think one person's opinion counts as idiomatic, but I'd probably use

// transform keys only (use same values)
hashMap.mapKeys { it.key.uppercase() }

// transform values only (use same key) - what you're after!
hashMap.mapValues { it.value.uppercase() }

// transform keys + values
hashMap.entries.associate { it.key.uppercase() to it.value.uppercase() }

Note: or toUpperCase() prior to Kotlin 1.5.0


The toMap function seems to be designed for this:

hashMap.map { (key, value) ->
      key.toLowerCase() to value.toUpperCase()
    }.toMap()

It converts Iterable<Pair<K, V>> to Map<K, V>


You could use the stdlib mapValues function that others have suggested:

hashMap.mapValues { it.value.uppercase() }

or with destructuring

hashMap.mapValues { (_, value) -> value.uppercase() }

I believe this is the most idiomatic way.