How to convert Scala's scala.List
into Java's java.util.List
?
Scala List and Java List are two different beasts, because the former is immutable and the latter is mutable. So, to get from one to another, you first have to convert the Scala List into a mutable collection.
On Scala 2.7:
import scala.collection.jcl.Conversions.unconvertList
import scala.collection.jcl.ArrayList
unconvertList(new ArrayList ++ List(1,2,3))
From Scala 2.8 onwards:
import scala.collection.JavaConversions._
import scala.collection.mutable.ListBuffer
asList(ListBuffer(List(1,2,3): _*))
val x: java.util.List[Int] = ListBuffer(List(1,2,3): _*)
However, asList
in that example is not necessary if the type expected is a Java List
, as the conversion is implicit, as demonstrated by the last line.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments