将 Java 集合转换为 Scala 集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/674713/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:44:16  来源:igfitidea点击:

Converting a Java collection into a Scala collection

javascalascala-collectionsscala-2.7

提问by oxbow_lakes

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

与堆栈溢出问题Scala 等效于 new HashSet(Collection) 相关,如何将 Java 集合(java.util.List例如)转换为 Scala 集合List

I am actually trying to convert a Java API call to Spring'sSimpleJdbcTemplate, which returns a java.util.List<T>, into a Scala immutable HashSet. So for example:

我实际上是在尝试将 Java API 调用转换为Spring 的SimpleJdbcTemplate,它返回java.util.List<T>, 到 Scala immutable HashSet。例如:

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

采纳答案by Jorge Ortiz

Your last suggestion works, but you can also avoid using jcl.Buffer:

您的最后一个建议有效,但您也可以避免使用jcl.Buffer

Set(javaApi.query(...).toArray: _*)

Note that scala.collection.immutable.Setis made available by default thanks to Predef.scala.

请注意,scala.collection.immutable.Set由于Predef.scala.

回答by Fabian Steeg

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

您可以将 Java 集合转换为数组,然后从中创建一个 Scala 列表:

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

回答by Aaron

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

您可以在 toArray 调用中添加类型信息以使 Set 参数化:

 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

这可能更可取,因为 collections 包正在为 Scala 2.8 进行重大修改,而 scala.collection.jcl 包正在消失

回答by Surya Suravarapu

You may also want to explore this excellent library: scalaj-collectionthat 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:

您可能还想探索这个优秀的库:scalaj-collection,它在 Java 和 Scala 集合之间进行双向转换。在您的情况下,要将 java.util.List 转换为 Scala List,您可以执行以下操作:

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

回答by jamesqiu

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

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

回答by robinst

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

供将来参考:使用 Scala 2.8,可以这样做:

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

setis a scala.collection.immutable.Set[String]after this.

setscala.collection.immutable.Set[String]在这之后。

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

另请参阅Ben James 的答案以获得更明确的方式(使用 JavaConverters),现在似乎推荐使用这种方式。

回答by Ben James

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

如果您想比robinst's answer 中演示的JavaConversions更明确,您可以使用 JavaConverters:

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

回答by aleksandr_hramcov

Another simple way to solve this problem:

解决此问题的另一种简单方法:

import collection.convert.wrapAll._

回答by stempler

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

JavaConversionsrobinst 的回答)和JavaConvertersBen James 的回答)已被Scala 2.10弃用。

Instead of JavaConversionsuse:

而不是JavaConversions使用:

import scala.collection.convert.wrapAll._

as suggested by aleksandr_hramcov.

正如aleksandr_hramcov所建议的那样。

Instead of JavaConvertersuse:

而不是JavaConverters使用:

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.:

对于两者,也可以分别将转换/转换器导入 Java 或 Scala,例如:

import scala.collection.convert.wrapAsScala._

Update:The statement above that JavaConversionsand JavaConverterswere 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.

更新:上面JavaConversionsJavaConverters已被弃用的声明似乎是错误的。Scala 2.10 中有一些不推荐使用的属性,这导致在导入它们时出现不推荐使用的警告。所以这里的替代导入似乎只是别名。虽然我仍然更喜欢它们,但恕我直言,这些名字更合适。

回答by Xavier Guihot

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

开始Scala 2.13,包scala.jdk.CollectionConverters替换包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")