With a Map of <String, Int>, is there an ele...
# getting-started
k
With a Map of <String, Int>, is there an elegant way to increment the Int if present, or set it to 0 if not?
t
maybe
foo.put("key", foo.getOrPut("key") { -1 } + 1)
👎 1
s
If you are on the jvm you can use
Copy code
.compute("key") { k, v ->
    if (v != null){
        v + 1
    } else 0
}