Back to questions
JuniorTheoryCommon
What does the program print?
fun main() {
val m = mutableMapOf("a" to 1)
m["b"] = 3
m["a"] = 2
println(m)
}{a=2, b=3}
- Expecting
{a=1, b=3, a=2}: a map holds each key once, so writing to"a"again replaces its value instead of adding a seconda. - Expecting the replaced key to move to the end:
mutableMapOfkeeps the order in which keys first arrived, soastays beforeb.
- How would you add 1 to the value under
"a"instead of replacing it? - What does
m.remove("a")change in the printed map?