oxbow_lakes oxbow_lakes - 1 year ago 67
Scala Question

Converting a Java collection into a Scala collection

Related to Stack Overflow question Scala equivalent of new HashSet(Collection) , how do I convert a Java collection (

say) into a Scala collection

I am actually trying to convert a Java API call to Spring's
, which returns a
, into a Scala immutable
. So for example:

val l: java.util.List[String] = javaApi.query( ... )
val s: HashSet[String] = //make a set from l

This seems to work. Criticism is welcome!

import scala.collection.immutable.Set
import scala.collection.jcl.Buffer
val s: scala.collection.Set[String] =
Set(Buffer(javaApi.query( ... ) ) : _ *)

Answer Source

Your last suggestion works, but you can also avoid using jcl.Buffer:

Set(javaApi.query(...).toArray: _*)

Note that scala.collection.immutable.Set is made available by default thanks to Predef.scala.