I have a MutableMap<String, MutableList<String> I'm adding items to, basically a collection where the same key is associated to several values.
Whenever I want to add a new value, I need to check first if there already is a list associated to the same key, initialize one if there isn't, and then add the value to the list.
I can do this in a pretty verbose way by doing
if (map.containsKey(key)) {
map[key].add(value)
} else {
map[key] = mutableListOf(value)
}
I also can do this in a very concise way by doing
map[key] = (map[key] ?: mutableListOf()) + mutableListOf(value)).toMutableList()
And in several other ways in between in terms of verbosity vs. conciseness.
What is the idiomatic way to do this in Kotlin, though?
I'm not really aiming for conciseness but for a form that is immediately recognizable and understandable.