Recall & Review
beginner
What is a Map transformation in Kotlin?
A Map transformation in Kotlin means creating a new Map by changing the keys, values, or both from an existing Map without modifying the original one.
Click to reveal answer
beginner
How do you transform only the values of a Map in Kotlin?
You use the
mapValues function, which applies a transformation to each value while keeping the keys the same.Click to reveal answer
beginner
What does the
mapKeys function do in Kotlin?The
mapKeys function creates a new Map by transforming the keys of the original Map while keeping the values unchanged.Click to reveal answer
beginner
Show a simple example of transforming a Map's values by doubling them.
Given
val map = mapOf("a" to 1, "b" to 2), <br> val doubled = map.mapValues { it.value * 2 } <br> Result: {"a"=2, "b"=4}Click to reveal answer
intermediate
Can you transform both keys and values of a Map at the same time in Kotlin?
Yes, by using the
map function on the Map's entries and then converting the result back to a Map with toMap().Click to reveal answer
Which Kotlin function transforms only the values of a Map?
✗ Incorrect
The
mapValues function changes only the values, keeping keys the same.What does
mapKeys do?✗ Incorrect
mapKeys changes keys but keeps values unchanged.How do you transform both keys and values of a Map?
✗ Incorrect
Using
map on entries lets you change both keys and values, then toMap() converts back to a Map.What is the result of
mapOf("x" to 3).mapValues { it.value + 1 }?✗ Incorrect
The value 3 is increased by 1, resulting in 4.
Which function keeps the original Map unchanged and returns a new transformed Map?
✗ Incorrect
Both
mapValues and mapKeys return new Maps without changing the original.Explain how to transform only the values of a Map in Kotlin and give a simple example.
Think about a function that changes values but keeps keys the same.
You got /4 concepts.
Describe how to transform both keys and values of a Map in Kotlin.
Use map on entries and then convert back to Map.
You got /4 concepts.