I wonder how to idiomatically iterate a java.util.HashSet
in Scala. Currently, I am using the java iterators in a while loop which does not seem to be great.
Additionally, I wonder if the mutable growable buffer is efficient or if there is a possibility to avoid the creation of unnecessary objects.
import java.util
import scala.collection.generic.Growable
import scala.collection.mutable
val javaSet = new util.HashSet[String]()
javaSet.add("first")
javaSet.add("second")
val result: collection.Seq[String] with Growable[String] = mutable.Buffer[String]()
val itr = javaSet.iterator
while (itr.hasNext) {
result += itr.next
}
result
edit
Would a stream
be better? Apache Spark: Effectively using mapPartitions in Java