0

I have a mutable map

val weeklyCheck = mutableMapOf(
    Day.MONDAY to true,
    Day.TUESDAY to true,
    Day.WEDNESDAY to true,
    Day.THURSDAY to true,
    Day.FRIDAY to true,
    Day.SATURDAY to true,
    Day.SUNDAY to true
)

How do I set all the keys to false. Currently I am using something like this, is there a better way to do this.

private fun resetDays() {
    weeklyCheck.put(Days.MONDAY, false)
    weeklyCheck.put(Days.TUESDAY, false)
    weeklyCheck.put(Days.WEDNESDAY, false)
    weeklyCheck.put(Days.THURDSAY, false)
    weeklyCheck.put(Days.FRIDAY, false)
    weeklyCheck.put(Days.SATURDAY, false)
    weeklyCheck.put(Days.SUNDAY, false)
}
A-run
  • 470
  • 4
  • 13
  • 1
    I'd find this question clearer if it asked about setting all the _values_ to false — or, equivalently, about setting all the keys to _map_ to false. – gidds Jun 08 '21 at 08:39

2 Answers2

7

You can use replaceAll - ignore the given key and value, and just return false no matter what. This will replace everything with false.

weeklyCheck.replaceAll { _, _ -> false }
Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

This can be used to achieve same results:

Days.values().forEach { weekFilter[it] = false }
A-run
  • 470
  • 4
  • 13