Hi! Anybody know what i should replace this with? ...
# announcements
m
Hi! Anybody know what i should replace this with? Or how to add default value = 0
Copy code
var harvest : HashMap<String,Int> = HashMap()
harvest[plant.name] += amount
s
See if
compute()
isnt what you need
e
triple backticks on multi lines
j
h
exactly.
Copy code
val harvest = mutableMapOf<String, Int>().withDefault { 0 }
// getValue is necessary to use the default set above:
harvest[plant.name] = harvest.getValue(plant.name) + amount
m
Thank you!
s
There is also
getOrElse
which I personally prefer to
withDefault
if there is only one place that retrieves values.
1