将 Java 集合转换为 Scala 集合
与 Stack Overflow 问题相关Scala 相当于新的 HashSet(Collection),如何转换 Java 集合(java.util.List
说)到 Scala 集合 List
中?
我实际上正在尝试将 Java API 调用转换为 Spring 的 SimpleJdbcTemplate
,它返回一个 java.util.ListHashSet
中。 例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您想比 robinst 的答案,你可以使用JavaConverters:
If you want to be more explicit than the JavaConversions demonstrated in robinst's answer, you can use JavaConverters:
JavaConversions(robinst 的回答)和 JavaConverters(Ben James 的回答) 已在 Scala 2.10 中弃用。
而不是 JavaConversions 使用:
按照 aleksandr_hramcov 的建议。
使用JavaConverters代替:
对于两者,也可以分别将转换/转换器导入Java或Scala,例如:
更新:
上面关于 JavaConversions 和 JavaConverters 已被弃用的说法似乎是错误的。 Scala 2.10 中有一些已弃用的属性,导致导入它们时出现弃用警告。 所以这里的备用导入似乎只是别名。 尽管我仍然更喜欢它们,但恕我直言,这些名字更合适。
JavaConversions (robinst's answer) and JavaConverters (Ben James's answer) have been deprecated with Scala 2.10.
Instead of JavaConversions use:
as suggested by aleksandr_hramcov.
Instead of JavaConverters use:
For both there is also the possibility to only import the conversions/converters to Java or Scala respectively, e.g.:
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.
您的最后一个建议有效,但您也可以避免使用
jcl.Buffer
:请注意,由于
Predef,默认情况下可以使用
scala.collection.immutable.Set
。斯卡拉。Your last suggestion works, but you can also avoid using
jcl.Buffer
:Note that
scala.collection.immutable.Set
is made available by default thanks toPredef.scala
.您可能还想探索这个优秀的库:scalaj-collection,它具有 Java 之间的双向转换和 Scala 集合。 在您的情况下,要将 java.util.List 转换为 Scala List,您可以执行以下操作:
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:
从 Scala 2.13 开始,包
scala.jdk.CollectionConverters
替换包scala.collection.JavaConverters/JavaConversions._
:Starting
Scala 2.13
, packagescala.jdk.CollectionConverters
replaces packagesscala.collection.JavaConverters/JavaConversions._
:您可以在 toArray 调用中添加类型信息以使 Set 参数化:
这可能更可取,因为 Collections 包正在经历 Scala 2.8 的重大修改,并且 scala.collection.jcl 包是 离开
You can add the type information in the toArray call to make the Set be parameterized:
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
您可以将 Java 集合转换为数组,然后从中创建一个 Scala 列表:
You could convert the Java collection to an array and then create a Scala list from that:
解决此问题的另一种简单方法:
Another simple way to solve this problem:
供将来参考:在 Scala 2.8 中,可以这样做:
set
在此之后是一个scala.collection.immutable.Set[String]
。另请参阅 Ben James 的回答以获得更明确的方式( using JavaConverters),现在似乎推荐使用它。
For future reference: With Scala 2.8, it could be done like this:
set
is ascala.collection.immutable.Set[String]
after this.Also see Ben James' answer for a more explicit way (using JavaConverters), which seems to be recommended now.