如何将 scala.List 转换为 java.util.List?

发布于 2024-08-24 10:55:57 字数 75 浏览 8 评论 0原文

如何将Scala的scala.List转换为Java的java.util.List

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

挥剑断情 2024-08-31 10:55:57

不知道为什么之前没有提到这一点,但我认为最直观的方法是调用 JavaConverters 直接位于 Scala 列表中:

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]

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]
伴随着你 2024-08-31 10:55:57

Scala List 和 Java List 是两种不同的东西,因为前者是不可变的,后者是可变的。因此,要从一个列表转换到另一个列表,您首先必须将 Scala 列表转换为可变集合。

在 Scala 2.7 上:

import scala.collection.jcl.Conversions.unconvertList
import scala.collection.jcl.ArrayList
unconvertList(new ArrayList ++ List(1,2,3))

从 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): _*)

但是,如果预期类型是 Java List,则该示例中的 asList 不是必需的,因为转换是隐式的,如以下所示最后一行。

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.

青芜 2024-08-31 10:55:57

总结之前的答案

假设我们有以下列表

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]

如果您不想共同控制转换并让编译器为您隐式工作:

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]

这取决于您想要如何控制代码。

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.

幻梦 2024-08-31 10:55:57

从 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]

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]
娇女薄笑 2024-08-31 10:55:57

尽管我会回答,但大多数建议都已被弃用,这是相当老的问题。

import scala.collection.JavaConversions.seqAsJavaList

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

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)
鯉魚旗 2024-08-31 10:55:57

更新

使用 scala 2.9.2

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

:结果

[1, 2, 3]

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]
无妨# 2024-08-31 10:55:57

对于单次调用,手动执行可能是最简单的解决方案:

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

我没有测量性能。

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.

这个俗人 2024-08-31 10:55:57

即使在 Java 端,只要按照上面的建议进行操作也会产生不可变列表。
我发现的唯一可行的解​​决方案是:

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

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
 }
与往事干杯 2024-08-31 10:55:57

自 Scala 2.12.0 JavaConversions 已被弃用。< /a>

所以对我来说最简单的解决方案是:

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

Since Scala 2.12.0 JavaConversions has been deprecated.

So the simplest solution for me was :

java.util.Arrays.asList("a","b","c")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文