U Avalos
U Avalos

Reputation: 6808

Idiomatic way to transform map in kotlin?

In Scala, it's just the map function. For example, if hashMap is a hashMap of strings, then you can do the following:

val result : HashMap[String,String] = hashMap.map(case(k,v) => (k -> v.toUpperCase))

In Kotlin, however, map turns the map into a list. Is there an idiomatic way of doing the same thing in Kotlin?

Upvotes: 98

Views: 69847

Answers (4)

James Bassett
James Bassett

Reputation: 9928

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

Upvotes: 206

Ruckus T-Boom
Ruckus T-Boom

Reputation: 4806

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.

Upvotes: 9

emu
emu

Reputation: 1683

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>

Upvotes: 34

Alexander Usikov
Alexander Usikov

Reputation: 477

I found another variant. It seems to be more clear

val result = mapOf( *hashMap.map { it.key.toUpperCase() to it.value.toUpperCase() }.toTypedArray() ) 

It'll automatically infer the type of resulted map.

.toTypedArray() is required to use splat(*) operator.

Upvotes: -4

Related Questions