Given that default implementation of a Set is immutable:
val Set = immutable.Set
And in order to make it mutable one needs to import
import scala.collection.mutable.Set;
In event one needs to use both mutable and immutable Sets in a given file, how should one go about it?
When you need to use both mutable and immutable collections in the same file, the canonical solution is just to prefix with mutable
or immutable
explicitly.
import collection._
val myMutableSet: mutable.Set[Int] = mutable.Set(1, 2, 3)
val myImmutableSet: immutable.Set[Int] = immutable.Set(1, 2, 3)
AS Kim Stebel mentioned in his answer, you can also use a renaming import:
import scala.collection.mutable.{Set => MutableSet}
However mutable.Set
is only one character more than MutableSet
, and does not introduce any new name so you might as well just use the former form.
You can rename symbols when you import them.
import scala.collection.mutable.{Set => MutableSet}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With