Martin Wangen
02/21/2019, 12:58 PMvar harvest : HashMap<String,Int> = HashMap()
harvest[plant.name] += amount
spand
02/21/2019, 1:00 PMcompute()
isnt what you needelect
02/21/2019, 1:36 PMJonathan Mew
02/21/2019, 1:36 PMhho
02/21/2019, 1:43 PMval harvest = mutableMapOf<String, Int>().withDefault { 0 }
// getValue is necessary to use the default set above:
harvest[plant.name] = harvest.getValue(plant.name) + amount
Martin Wangen
02/21/2019, 1:47 PMspand
02/21/2019, 2:11 PMgetOrElse
which I personally prefer to withDefault
if there is only one place that retrieves values.