Kotlin MutableMap.putAll
Apply a set of weekend overrides to the normal opening schedule.
A simple example
MutableMap.putAll
- Start with A = 1, B = 2
- Do this Merge B = 3 and C = 4
- Result A = 1, B = 3, C = 4
Syntax
hours.putAll(overrides)
Run these examples in Kotlin
Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.
kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jarApply weekend opening hours
A library keeps standard opening hours for every day, then receives a short list of weekend changes. The scheduler applies those overrides while preserving weekdays that the weekend list never mentions.
fun main() {
val hours = mutableMapOf("Mon" to 9, "Sat" to 9)
hours.putAll(mapOf("Sat" to 10, "Sun" to 11))
println(hours.getValue("Sat"))
}
10
The Saturday override changes the displayed hour to ten.
Merge weekend opening hours
The branch receives an updated Saturday timetable. A later set of assignments is merged into the first. Which value does the overlapping key hold?
Merge a volunteer rota
The organizer imports a replacement Saturday volunteer. A later set of assignments is merged into the first. Which value does the overlapping key hold?
What to watch for
New values overwrite existing keys. Nested mutable objects are shared when the map values are references.