How to use mutable and immutable sets in the same file in Scala

Given that the standard implementation of Set is immutable:

val Set = immutable.Set 

And to make it volatile, you need to import

 import scala.collection.mutable.Set; 

If necessary, use both mutable and immutable Sets in this file, how can this be done?

+6
source share
2 answers

If you need to use both mutable and immutable collections in the same file, the canonical solution is simply for the mutable or immutable prefix 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 noted in his answer, you can also use rename imports:

 import scala.collection.mutable.{Set => MutableSet} 

However, mutable.Set is only one character larger than MutableSet , and does not introduce any new name so you can simply use the old form.

+11
source

You can rename characters when importing.

 import scala.collection.mutable.{Set => MutableSet} 
+9
source

All Articles