Converting a Java collection into a Scala collection

JavaScalaScala CollectionsScala 2.7

Java Problem Overview


Related to Stack Overflow question Scala equivalent of new HashSet(Collection) , how do I convert a Java collection (java.util.List say) into a Scala collection List?

I am actually trying to convert a Java API call to Spring's SimpleJdbcTemplate, which returns a java.util.List<T>, into a Scala immutable HashSet. 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( ... ) ) : _ *)

Java Solutions


Solution 1 - Java

For future reference: With Scala 2.8, it could be done like this:

import scala.collection.JavaConversions._
val list = new java.util.ArrayList[String]()
list.add("test")
val set = list.toSet

set is a scala.collection.immutable.Set[String] after this.

Also see Ben James' answer for a more explicit way (using JavaConverters), which seems to be recommended now.

Solution 2 - Java

If you want to be more explicit than the JavaConversions demonstrated in robinst's answer, you can use JavaConverters:

import scala.collection.JavaConverters._
val l = new java.util.ArrayList[java.lang.String]
val s = l.asScala.toSet

Solution 3 - Java

JavaConversions (robinst's answer) and JavaConverters (Ben James's answer) have been deprecated with Scala 2.10.

Instead of JavaConversions use:

import scala.collection.convert.wrapAll._

as suggested by aleksandr_hramcov.

Instead of JavaConverters use:

import scala.collection.convert.decorateAll._

For both there is also the possibility to only import the conversions/converters to Java or Scala respectively, e.g.:

import scala.collection.convert.wrapAsScala._

Update: The statement above that JavaConversions and JavaConverters were deprecated seems to be wrong. There were some deprecated properties in Scala 2.10, which resulted in deprecation warnings when importing them. So the alternate imports here seem to be only aliases. Though I still prefer them, as IMHO the names are more appropriate.

Solution 4 - Java

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.

Solution 5 - Java

You may also want to explore this excellent library: scalaj-collection that has two-way conversion between Java and Scala collections. In your case, to convert a java.util.List to Scala List you can do this:

val list = new java.util.ArrayList[java.lang.String]
list.add("A")
list.add("B")
list.asScala

Solution 6 - Java

Starting Scala 2.13, package scala.jdk.CollectionConverters replaces packages scala.collection.JavaConverters/JavaConversions._:

import scala.jdk.CollectionConverters._

// val javaList: java.util.List[String] = java.util.Arrays.asList("one","two","three")
javaList.asScala
// collection.mutable.Buffer[String] = Buffer("one", "two", "three")
javaList.asScala.toSet
// collection.immutable.Set[String] = Set("one", "two", "three")

Solution 7 - Java

val array = java.util.Arrays.asList("one","two","three").toArray

val list = array.toList.map(_.asInstanceOf[String])

Solution 8 - Java

You can add the type information in the toArray call to make the Set be parameterized:

 val s = Set(javaApi.query(....).toArray(new Array[String](0)) : _*)

This might be preferable as the collections package is going through a major rework for Scala 2.8 and the scala.collection.jcl package is going away

Solution 9 - Java

You could convert the Java collection to an array and then create a Scala list from that:

val array = java.util.Arrays.asList("one","two","three").toArray
val list = List.fromArray(array)

Solution 10 - Java

Another simple way to solve this problem:

import collection.convert.wrapAll._

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
Questionoxbow_lakesView Question on Stackoverflow
Solution 1 - JavarobinstView Answer on Stackoverflow
Solution 2 - JavaBen JamesView Answer on Stackoverflow
Solution 3 - JavastemplerView Answer on Stackoverflow
Solution 4 - JavaJorge OrtizView Answer on Stackoverflow
Solution 5 - JavaSurya SuravarapuView Answer on Stackoverflow
Solution 6 - JavaXavier GuihotView Answer on Stackoverflow
Solution 7 - JavajamesqiuView Answer on Stackoverflow
Solution 8 - JavaAaronView Answer on Stackoverflow
Solution 9 - JavaFabian SteegView Answer on Stackoverflow
Solution 10 - Javaaleksandr_hramcovView Answer on Stackoverflow