if I’m using the Scala Multimap, and I want to get the values associated with a key or else the empty set, do I have to write the following?
multimap.getOrElse("key", new collection.mutable.HashSet())
It would seem that the following should just work. An empty set seems like a good default value.
multimap.getOrElse("key")
As you observed, the
MultiMaptrait doesn’t do what you want. However, you can add a default value yourself if the Map is specifically mutable or immutable. Here’s an example,Strangely, the above won’t work if the type of
mis an abstractcollection.Map. The comment in the source code says this is due to variance issues.