Hi guys, given I have a map like ```val myMap = so...
# getting-started
a
Hi guys, given I have a map like
Copy code
val myMap = sortedMapOf<String, Collection<Int>>()
Is there a way to put into the map and overflow in the collection in case the key already exist in the map?
d
Copy code
myMap.getOrPut(key) { MyCollection() } += myValue
a
thanks!
n
to perhaps state the obvious, make sure it's a mutable collection :)
👍 1
a
only thing is, it seems I can’t really do
myMap.getOrPut(key) { MyCollection() } += myValue
kotlin expects that I put the list in a val before doing +=
d
Make sure both your map and your collection are mutable. In my test it works fine: https://pl.kotl.in/psAZfS9uZ
a
yeah I can use .add() but not += for some reasons, but that’s fine 🙂