How to convert a scala.List to a java.util.List?

JavaScalaScala Java-Interop

Java Problem Overview


How to convert Scala's scala.List into Java's java.util.List?

Java Solutions


Solution 1 - Java

Not sure why this hasn't been mentioned before but I think the most intuitive way is to invoke the asJava decorator method of JavaConverters directly on the Scala list:

scala> val scalaList = List(1,2,3)
scalaList: List[Int] = List(1, 2, 3)

scala> import scala.collection.JavaConverters._
import scala.collection.JavaConverters._

scala> scalaList.asJava
res11: java.util.List[Int] = [1, 2, 3]

Solution 2 - Java

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.

Solution 3 - Java

To sum up the previous answers

Assuming we have the following List:

scala> val scalaList = List(1,2,3)
scalaList: List[Int] = List(1, 2, 3)

If you want to be explicit and tell exactly what you want to convert:

scala> import scala.collection.JavaConverters._
import scala.collection.JavaConverters._

scala> scalaList.asJava
res11: java.util.List[Int] = [1, 2, 3]

If you don't want co control conversions and let compiler make implicit work for you:

scala> import scala.collection.JavaConversions._
import scala.collection.JavaConversions._

scala> val javaList: java.util.List[Int] = scalaList
javaList: java.util.List[Int] = [1, 2, 3]

It's up to you how you want to control your code.

Solution 4 - Java

Starting Scala 2.13, the package scala.jdk.CollectionConverters provides asJava via a pimp of Seq and replaces packages scala.collection.JavaConverters/JavaConversions:

import scala.jdk.CollectionConverters._

// val scalaList: List[Int] = List(1, 2, 3)
scalaList.asJava
// java.util.List[Int] = [1, 2, 3]

Solution 5 - Java

Pretty old questions, though I will answer, given but most of suggestions are deprecated.

import scala.collection.JavaConversions.seqAsJavaList

val myList = List("a", "b", "c")
val myListAsJavaList = seqAsJavaList[String](myList)

Solution 6 - Java

Update

with scala 2.9.2:

import scala.collection.JavaConversions._
import scala.collection.mutable.ListBuffer
val x: java.util.List[Int] = ListBuffer( List( 1, 2, 3 ): _* )

result

[1, 2, 3]

Solution 7 - Java

For single invocations, doing it by hand might be the simplest solution:

val slist = List (1, 2, 3, 4)          
val jl = new java.util.ArrayList [Integer] (slist.size)
slist.foreach (jl.add (_))   

I didn't measure performance.

Solution 8 - Java

Just doing as proposed above produces immutable list even on Java side. The only working solution I've found is this:

def toJList[T](l:List[T]):util.List[T] = {
  val a = new util.ArrayList[T]
  l.map(a.add(_))
  a
 }

Solution 9 - Java

Since Scala 2.12.0 JavaConversions has been deprecated.

So the simplest solution for me was :

java.util.Arrays.asList("a","b","c")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex BaranoskyView Question on Stackoverflow
Solution 1 - JavadimitrisliView Answer on Stackoverflow
Solution 2 - JavaDaniel C. SobralView Answer on Stackoverflow
Solution 3 - JavaKamil LelonekView Answer on Stackoverflow
Solution 4 - JavaXavier GuihotView Answer on Stackoverflow
Solution 5 - JavaShirish KumarView Answer on Stackoverflow
Solution 6 - JavaGuillaume MasséView Answer on Stackoverflow
Solution 7 - JavaStefan W.View Answer on Stackoverflow
Solution 8 - JavaVitamonView Answer on Stackoverflow
Solution 9 - JavaelaribView Answer on Stackoverflow