Hi there, can I ask how to achieve this in a bette...
# getting-started
Hi there, can I ask how to achieve this in a better way?:
Copy code
val map = mutableMapOf<String, MutableSet<String>>()

    fun putValueToSet(key: String, value:String) {
        val set = map[key] ?: mutableSetOf<String>()
        map.put(key, set)
Suppose I need to call this
many times and sometimes the key is new and I need to create a new Set before I could add value into it. But the current code calls on
map.put(key, set)
all the time and it’s inefficient as many case I already have the Set in map with a key. I thought I could add some boolean and if to check but feels this not like the kotlin way. Can you suggest something better? I feel I need a function like
map.getOrCreateDefault(key, mutableSetOf())
Or in general, if I want to have a Table, or a Map of Map where I could freely put new or seen values into it, how do I achieve that as the best way?