score:17
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
.
score:1
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)
score:1
Another simple way to solve this problem:
import collection.convert.wrapAll._
score:2
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
score:3
val array = java.util.Arrays.asList("one","two","three").toArray
val list = array.toList.map(_.asInstanceOf[String])
score:6
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")
score:14
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
score:25
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.
score:62
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
score:127
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.
Source: stackoverflow.com
Related Query
- Converting a Java collection into a Scala collection
- Converting a Java map into a Scala immutable map in Java code
- Problems converting collection types from scala to java
- UnmodifiableRandomAccessList exception converting collection from Java to Scala
- Scala best way of turning a Collection into a Map-by-key?
- Converting Java to Scala durations
- How are Scala traits compiled into Java bytecode?
- Convert Scala Set into Java (java.util.Set)?
- Integrating Scala into an existing project in Java
- How to convert a nested scala collection to a nested Java collection
- Transforming Scala varargs into Java Object... varargs
- Converting Scala @suspendable Method into a Future
- Why is a Scala companion object compiled into two classes(both Java and .NET compilers)?
- Java <-> Scala Collection conversions, Scala 2.10
- How do I convert a Java byte array into a Scala byte array?
- Scala best way of turning a Collection into a Map-by-key? (2nd variant)
- Scala Spark: Split collection into several RDD?
- Use a Scala collection method to help convert a list of [0,0,0,1,1,1,1,0,0,1,1] into [3,4,2,2]
- Convert Java array to Scala collection
- How to create a Scala parallel collection from a Java collection
- In Scala - Converting a case class List into a List of tuples
- Converting a (List of Future of Either) into a (Future of Either of List) in Scala
- What's the cost of converting a sequential collection into a parallel one, against creating it from scratch
- How to convert a collection conversion using java to scala
- Translate Flink scala into java
- Weaving Scala into existing Java EE projects?
- Converting nested Scala type to Java types
- how to decode Java strings with Unicode escapes etc. from Scala JavaTokenParsers into unescaped strings?
- How to convert Scala collection Seq[(Int, Seq[String])] to Java collection List[(int, List[String])]?
- In Scala, how do I mixin java interfaces into Scala code
More Query from same tag
- Filtering a collection based on an arbitrary number of options
- For debugging I would like println in functions with explicit return types
- How to convert DStream of number of RDDs to Single RDD
- Scala: pattern matching with reusable condition
- how to use a variable as spark selected fields
- How to access a Scala object instance given it's full qualified name?
- MatchersException despite using any() and eq() in Test [Scala]
- making an implicit available within a play action
- Scala: Quicksort for a MutableList[Array[Double]]
- Play Framework 2.2.0 [scala] - WebSocket.async vs WebSocket.using[T]
- How can I ignore empty objects with play-json?
- Append to environment variable for Scala Process
- Are implicit conversions for Scala numeric types special?
- Convert Scala Dataframe to HashMap
- Identifying two type wildcards as identical
- How to build a large distributed [sparse] matrix in Apache Spark 1.0?
- How to replace all the numbers with literal \d in scala?
- How to filter Date Columns and store them as numbers in Data Frames using Scala
- Setting Whitespace as Delimiter in JavaTokenParsers
- How to define an Ordering in Scala?
- Explain ListBuffer(comp: _*)
- Java Play 2.2.6 : index.scala.html update not reflecting in controller.Application
- Play (Scala) Custom form type validation
- In Scala, how can I restrict multiple parameters must be a type inside the same type?
- Scala equivalent for Javas stream findFirst()
- How to parse a json with date exported by mongoexport in Scala?
- Scala on Eclipse gives errors on Map operations
- Play Framework Anorm & DB Not Resolved
- Scala and dropWhile
- scala - immutability performances