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

Alex Baranosky :

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

Daniel C. Sobral :

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert a java.util.List to a Scala list

How to convert a Scala Iterable into Java util.List?

unable to convert a java.util.List into Scala list

How to convert java.util.List[java.lang.Long], to java.util.List[Long]

How to convert Scala Array to Java List ?

How to convert a Java Collection/List to a Scala seq?

How to convert Scala List to Java ArrayList

How to convert java list to array in scala?

How to convert Scala List[String] to legacy Java List<?>

How to convert a List of List to a List in Scala?

Convert from scala.collection.Seq<String> to java.util.List<String> in Java code

Convert Java List to Scala Seq

Play! failing to convert java list to scala list

Convert List to Array without java.util

(Scala) How to convert List into a Seq

In Scala, how to convert FilterMonadic to a List?

"Failed to convert value of type 'java.lang.String' to required 'java.util.List'." Or how to convert Python list to Java.util list?

Implicit conversion of java.util.List to scala List does not occur

Best solution for converting java.util.List to Scala list

how to convert members of case class in Scala to List of Objects in Java

ModelMapper failed to convert java.util.List to java.util.List

convert scala hashmap with List to java hashmap with java list

How to get Scala List from Java List?

How to convert Scala collection Seq[(Int, Seq[String])] to Java collection List[(int, List[String])]?

How to convert a List[java.lang.Long] to a List[scala.Long]

Type mismatch, expected util.List[Nothing] from java to scala

Java Util Linked List - how to find next?

How to copy java.util.list Collection

How to convert List of List String into List of List Integer in java