I'm creating a Map to a Set, and I would like to h...
# getting-started
j
I'm creating a Map to a Set, and I would like to have a default value put into the map if the set does not exist. My solution is:
val map = mutableMapOf<String, MutableSet<String>>().withDefault { mutableSetOf() }
val key = "a"; val item = "b"
map.getOrPut(key, {map.getValue(key)}).add(item)
This works, but it feels verbose since I already defined the default value of the map. Is there a shortcut to make use of the default value? I wanted to just use:
map.getOrPut(key).add(item)