将 Java 集合转换为 Scala 集合

发布于 2024-07-15 02:17:06 字数 778 浏览 8 评论 0原文

与 Stack Overflow 问题相关Scala 相当于新的 HashSet(Collection),如何转换 Java 集合(java.util.List 说)到 Scala 集合 List 中?

我实际上正在尝试将 Java API 调用转换为 Spring 的 SimpleJdbcTemplate,它返回一个 java.util.List到 Scala 不可变的 HashSet 中。 例如:

val l: java.util.List[String] = javaApi.query( ... )
val s: HashSet[String] = //make a set from l

这似乎有效。 欢迎批评!

import scala.collection.immutable.Set
import scala.collection.jcl.Buffer 
val s: scala.collection.Set[String] =
                      Set(Buffer(javaApi.query( ... ) ) : _ *)

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

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

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

发布评论

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

评论(10

遗失的美好 2024-07-22 02:17:07

如果您想比 robinst 的答案,你可以使用JavaConverters:

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

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
Spring初心 2024-07-22 02:17:07

JavaConversionsrobinst 的回答)和 JavaConverters(Ben James 的回答) 已在 Scala 2.10 中弃用。

而不是 JavaConversions 使用:

import scala.collection.convert.wrapAll._

按照 aleksandr_hramcov 的建议。

使用JavaConverters代替:

import scala.collection.convert.decorateAll._

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

import scala.collection.convert.wrapAsScala._

更新:
上面关于 JavaConversionsJavaConverters 已被弃用的说法似乎是错误的。 Scala 2.10 中有一些已弃用的属性,导致导入它们时出现弃用警告。 所以这里的备用导入似乎只是别名。 尽管我仍然更喜欢它们,但恕我直言,这些名字更合适。

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.

没有伤那来痛 2024-07-22 02:17:07

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

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

请注意,由于Predef,默认情况下可以使用scala.collection.immutable.Set。斯卡拉。

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.

眼眸 2024-07-22 02:17:07

您可能还想探索这个优秀的库: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

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
故人的歌 2024-07-22 02:17:07

从 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")

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")
初与友歌 2024-07-22 02:17:07
val array = java.util.Arrays.asList("one","two","three").toArray

val list = array.toList.map(_.asInstanceOf[String])
val array = java.util.Arrays.asList("one","two","three").toArray

val list = array.toList.map(_.asInstanceOf[String])
笑忘罢 2024-07-22 02:17:07

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

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

这可能更可取,因为 Collections 包正在经历 Scala 2.8 的重大修改,并且 scala.collection.jcl 包是 离开

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

胡大本事 2024-07-22 02:17:07

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

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

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)
好多鱼好多余 2024-07-22 02:17:07

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

import collection.convert.wrapAll._

Another simple way to solve this problem:

import collection.convert.wrapAll._
最佳男配角 2024-07-22 02:17:06

供将来参考:在 Scala 2.8 中,可以这样做:

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

set 在此之后是一个 scala.collection.immutable.Set[String]

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

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.

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