https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

Alex

08/17/2022, 3:38 PM
I was trying to see if i could use the mapValues(playing around with it to understand how it works) to transform the map for both the String and number(indicated by x and y) i had to use a combination of Destructing Declaration to be able to use the 2nd value which is y But i had to add a ";" i'm not too sure if this is the correct way, hope someone can advise me if this is correct
Copy code
val aMap= mapOf("one" to 1,"two" to 2)



fun main() {
    
    aMap.mapValues { (z,y)-> "$"+z;"$"+y }.forEach{z->println(z)}

}
s

Sam

08/17/2022, 3:39 PM
Maybe you want something like this?
Copy code
aMap.entries.associate { (z, y) ->
   "$"+z to "$"+y
}
mapValues
will only change the values in the map, even though it receives both the key and the value in its inputs
The semicolon is not likely to be useful because it separates the line into two separate statements. The return value of a lambda function is always the final statement, so the part before the semicolon will actually end up being ignored.
When using
associate
you can instead use
to
to join the two parts into a
Pair
that’s returned in a single statement
a

Alex

08/17/2022, 5:32 PM
Thank you @Sam
11 Views