如何将 scala.List 转换为 java.util.List?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2429944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a scala.List to a java.util.List?
提问by Alex Baranosky
How to convert Scala's scala.List
into Java's java.util.List
?
如何将 Scala 转换scala.List
为 Java 的java.util.List
?
采纳答案by 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.
Scala List 和 Java List 是两种不同的野兽,因为前者是不可变的,而后者是可变的。因此,要从一个到另一个,您首先必须将 Scala 列表转换为可变集合。
On Scala 2.7:
在 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:
从 Scala 2.8 开始:
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.
但是,asList
在该示例中,如果预期类型是 Java 则没有必要List
,因为转换是隐式的,如最后一行所示。
回答by Stefan W.
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.
我没有衡量性能。
回答by Guillaume Massé
Update
更新
with scala 2.9.2:
使用 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]
回答by Vitamon
Just doing as proposed above produces immutable list even on Java side. The only working solution I've found is this:
即使在 Java 端,按照上面的建议去做也会产生不可变的列表。我找到的唯一可行的解决方案是:
def toJList[T](l:List[T]):util.List[T] = {
val a = new util.ArrayList[T]
l.map(a.add(_))
a
}
回答by dimitrisli
Not sure why this hasn't been mentioned before but I think the most intuitive way is to invoke the asJava
decorator method of JavaConvertersdirectly on the Scala list:
不知道为什么之前没有提到这一点,但我认为最直观的方法是直接在 Scala 列表上调用JavaConverters的asJava
装饰器方法:
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]
回答by Kamil Lelonek
To sum up the previous answers
总结一下之前的回答
Assuming we have the following List
:
假设我们有以下内容List
:
scala> val scalaList = List(1,2,3)
scalaList: List[Int] = List(1, 2, 3)
If you want to be explicitand tell exactlywhat 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 conversionsand let compiler make implicitwork 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.
由您决定如何控制代码。
回答by Shirish Kumar
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)
回答by elarib
Since Scala 2.12.0 JavaConversions has been deprecated.
自 Scala 2.12.0 JavaConversions 已被弃用。
So the simplest solution for me was :
所以对我来说最简单的解决方案是:
java.util.Arrays.asList("a","b","c")
回答by Xavier Guihot
Starting Scala 2.13
, the package scala.jdk.CollectionConverters
provides asJava
via a pimp of Seq
and replaces packages scala.collection.JavaConverters/JavaConversions
:
开始Scala 2.13
时,包scala.jdk.CollectionConverters
提供asJava
通过的皮条客Seq
,并替换套餐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]