Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
How does one convert a Scala Array to a mutable.Set?
Array
mutable.Set
It's easy to convert to an immutable.Set:
immutable.Set
Array(1, 2, 3).toSet
But I can't find an obvious way to convert to a mutable.Set.
scala> val s=scala.collection.mutable.Set()++Array(1,2,3) s: scala.collection.mutable.Set[Int] = Set(2, 1, 3)
Add a comment
scala> scala.collection.mutable.Set( Array(1,2) :_* ) res2: scala.collection.mutable.Set[Int] = Set(2, 1)
The weird :_* type ascription, forces factory method to see the array as a list of arguments.
:_*
Starting Scala 2.10, via factory builders applied with .to(factory):
Scala 2.10
.to(factory)
Array(1, 2, 3).to[collection.mutable.Set] // collection.mutable.Set[Int] = Set(1, 2, 3)
And starting Scala 2.13:
Scala 2.13
Array(1, 2, 3).to(collection.mutable.Set) // collection.mutable.Set[Int] = HashSet(1, 2, 3)
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.