0
0
Kotlinprogramming~5 mins

Map transformation in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AmapKeys
BmapValues
Cmap
Dfilter
What does mapKeys do?
AFilters keys
BTransforms values only
CTransforms both keys and values
DTransforms keys only
How do you transform both keys and values of a Map?
AUse mapValues and mapKeys together
BUse filter
CUse map on entries and then toMap()
DUse reduce
What is the result of mapOf("x" to 3).mapValues { it.value + 1 }?
A{"x"=4}
B{"x"=3}
C{"x"=1}
D{}
Which function keeps the original Map unchanged and returns a new transformed Map?
ABoth A and C
BmapValues
CmapKeys
DNone
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.