Frequently, I find myself implementing methods that build a map from an input source. In this case, I use a scala.collection.mutable.Map
, assuming greater speed and efficiency; however, once this collection is built, I no longer want it to be mutable.
What is the preferred Scala way of returning an immutable map from a mutable one? Usually, I do a myMap.toMap
which obviously works, but smells. Alternatively, you can set the return type to scala.collection.Map
which doesn't require building a new collection, but seems like it could confuse readers.
Thanks for any replies.
newBuilder
as in this other question and then.result
would be an equivalent solution with some style merits over defining a mutableMap
– matanster