Scala 中的 JavaConverters 和 JavaConversions 有什么区别?
在 scala.collection
中,有两个非常相似的对象 JavaConversions
和 <一个href="http://www.scala-lang.org/api/current/scala/collection/JavaConverters%24.html">JavaConverters
。
- 这两个对象有什么区别?
- 为什么他们都存在?
- 我什么时候想使用其中一种而不是另一种?
In scala.collection
, there are two very similar objects JavaConversions
and JavaConverters
.
- What is the difference between these two objects?
- Why do they both exist?
- When do I want to use one vs. the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:
Java Conversions
在 Scala 2.13.0 中得到了@deprecated
。请改用 scala.jdk.CollectionConverters 。JavaConversions
提供了一系列隐式方法,可以在 Java 集合和最接近的相应 Scala 集合之间进行转换,反之亦然。这是通过创建实现 Scala 接口并将调用转发到底层 Java 集合或 Java 接口并将调用转发到底层 Scala 集合的包装器来完成的。JavaConverters
使用 pimp-my-library 模式将asScala
方法“添加”到 Java 集合,并将asJava
方法“添加”到 Scala 集合,它返回上面讨论的适当的包装器。它比 JavaConversions(自 2.8 版起)更新(自版本 2.8.1 起),并且使 Scala 和 Java 集合之间的转换变得明确。与 David 在他的回答中所写的相反,我建议您养成使用 JavaConverters 的习惯,因为您不太可能编写进行大量隐式转换的代码,因为您可以控制发生这种情况的唯一位置:编写.asScala
或.asJava
的位置。以下是
JavaConverters
提供的转换方法:不过,要直接从 Java 使用转换,最好直接从
JavaConversions
调用方法;例如:EDIT:
Java Conversions
got@deprecated
in Scala 2.13.0. Use scala.jdk.CollectionConverters instead.JavaConversions
provide a series of implicit methods that convert between a Java collection and the closest corresponding Scala collection, and vice versa. This is done by creating wrappers that implement either the Scala interface and forward the calls to the underlying Java collection, or the Java interface, forwarding the calls to the underlying Scala collection.JavaConverters
uses the pimp-my-library pattern to “add” theasScala
method to the Java collections and theasJava
method to the Scala collections, which return the appropriate wrappers discussed above. It is newer (since version 2.8.1) thanJavaConversions
(since 2.8) and makes the conversion between Scala and Java collection explicit. Contrary to what David writes in his answer, I'd recommend you make it a habit to useJavaConverters
as you'll be much less likely to write code that makes a lot of implicit conversions, as you can control the only spot where that will happen: where you write.asScala
or.asJava
.Here's the conversion methods that
JavaConverters
provide:To use the conversions directly from Java, though, you're better off calling methods from
JavaConversions
directly; e.g.:对于自 Scala 2.12.x 以来遇到此问题的任何人,
JavaConversions
现已弃用,而JavaConverters
是首选方法。For anyone landing on this question since Scala 2.12.x,
JavaConversions
is now deprecated andJavaConverters
is the preferred method.在 Scala 2.13 中,
JavaConverters
已弃用,取而代之的是 <代码>scala.jdk.CollectionConverters:In Scala 2.13,
JavaConverters
have been deprecated in favour ofscala.jdk.CollectionConverters
:如 API 中所述,
JavaConversions
是一组隐式转换,将 java 集合转换为相关的 scala 集合。您可以将其与
import collection.JavaConversions._
一起使用。必要时,编译器会自动将java集合转换为正确的scala类型。JavaConverters
是一组装饰器,可帮助使用asScala
或asJava
方法将 java 或 scala 集合转换为 scala 或 java 集合,这些方法将隐式添加到要转换的集合中。为了使用这些转换器,您需要导入:您应该更喜欢
JavaConversions
因为它通常更容易使用(不需要使用asScala
或asJava
>)。As explained in the API,
JavaConversions
is a set of implicit conversions that transforms java collections into related scala collection.You can use it with an
import collection.JavaConversions._
. When necessary, the compiler will automatically transform the java collection into the right scala type.JavaConverters
are a set of decorator that helps transform java or scala collections to scala or java collections usingasScala
orasJava
methods that will be implicitly added to the collection that you want to transform. In order to use these converters, you need to import :You should prefer
JavaConversions
as it's generally easier to use (no need to useasScala
orasJava
).