I've been looking for a bit of time on how to convert an ArrayBuffer
to a Set
, an HashSet
I guess to be precise. Any hint?
There is a toSet
function implemented in ArrayBuffer
Example:
scala> import collection.mutable.ArrayBuffer
import collection.mutable.ArrayBuffer
scala> import collection.immutable.HashSet
import collection.immutable.HashSet
scala> val a = new ArrayBuffer(2)
a: scala.collection.mutable.ArrayBuffer[Nothing] = ArrayBuffer()
scala> val b = a.toSet
b: scala.collection.immutable.Set[Nothing] = Set()
To Set:
scala> val bf = ArrayBuffer(1,2,3,4)
bf: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)
scala> bf.toSet
res0: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
To HashSet:
scala> val hs = new HashSet[Int]++ bf.toSet
hs: scala.collection.immutable.HashSet[Int] = Set(1, 2, 3, 4)
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