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
}
Sam
08/17/2022, 3:40 PM
mapValues
will only change the values in the map, even though it receives both the key and the value in its inputs
Sam
08/17/2022, 3:50 PM
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.