Scala Language Java Interoperability Converting Scala Collections to Java Collections and vice versa


Example

When you need to pass a collection into a Java method:

import scala.collection.JavaConverters._

val scalaList = List(1, 2, 3)
JavaLibrary.process(scalaList.asJava)

If the Java code returns a Java collection, you can turn it into a Scala collection in a similar manner:

import scala.collection.JavaConverters._

val javaCollection = JavaLibrary.getList
val scalaCollection = javaCollection.asScala

Note that these are decorators, so they merely wrap the underlying collections in a Scala or Java collection interface. Therefore, the calls .asJava and .asScala do not copy the collections.