Scala 中的 JavaConverters 和 JavaConversions 有什么区别?

发布于 2024-12-18 22:59:41 字数 467 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(4

年少掌心 2024-12-25 22:59:41

编辑: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 提供的转换方法:

Pimped Type                            | Conversion Method   | Returned Type
=================================================================================================
scala.collection.Iterator              | asJava              | java.util.Iterator
scala.collection.Iterator              | asJavaEnumeration   | java.util.Enumeration
scala.collection.Iterable              | asJava              | java.lang.Iterable
scala.collection.Iterable              | asJavaCollection    | java.util.Collection
scala.collection.mutable.Buffer        | asJava              | java.util.List
scala.collection.mutable.Seq           | asJava              | java.util.List
scala.collection.Seq                   | asJava              | java.util.List
scala.collection.mutable.Set           | asJava              | java.util.Set
scala.collection.Set                   | asJava              | java.util.Set
scala.collection.mutable.Map           | asJava              | java.util.Map
scala.collection.Map                   | asJava              | java.util.Map
scala.collection.mutable.Map           | asJavaDictionary    | java.util.Dictionary
scala.collection.mutable.ConcurrentMap | asJavaConcurrentMap | java.util.concurrent.ConcurrentMap
—————————————————————————————————————————————————————————————————————————————————————————————————
java.util.Iterator                     | asScala             | scala.collection.Iterator
java.util.Enumeration                  | asScala             | scala.collection.Iterator
java.lang.Iterable                     | asScala             | scala.collection.Iterable
java.util.Collection                   | asScala             | scala.collection.Iterable
java.util.List                         | asScala             | scala.collection.mutable.Buffer
java.util.Set                          | asScala             | scala.collection.mutable.Set
java.util.Map                          | asScala             | scala.collection.mutable.Map
java.util.concurrent.ConcurrentMap     | asScala             | scala.collection.mutable.ConcurrentMap
java.util.Dictionary                   | asScala             | scala.collection.mutable.Map
java.util.Properties                   | asScala             | scala.collection.mutable.Map[String, String]

不过,要直接从 Java 使用转换,最好直接从 JavaConversions 调用方法;例如:

List<String> javaList = new ArrayList<String>(Arrays.asList("a", "b", "c"));
System.out.println(javaList); // [a, b, c]
Buffer<String> scalaBuffer = JavaConversions.asScalaBuffer(javaList);
System.out.println(scalaBuffer); // Buffer(a, b, c)
List<String> javaListAgain = JavaConversions.bufferAsJavaList(scalaBuffer);
System.out.println(javaList == javaListAgain); // true

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” the asScala method to the Java collections and the asJava method to the Scala collections, which return the appropriate wrappers discussed above. It is newer (since version 2.8.1) than JavaConversions (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 use JavaConverters 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:

Pimped Type                            | Conversion Method   | Returned Type
=================================================================================================
scala.collection.Iterator              | asJava              | java.util.Iterator
scala.collection.Iterator              | asJavaEnumeration   | java.util.Enumeration
scala.collection.Iterable              | asJava              | java.lang.Iterable
scala.collection.Iterable              | asJavaCollection    | java.util.Collection
scala.collection.mutable.Buffer        | asJava              | java.util.List
scala.collection.mutable.Seq           | asJava              | java.util.List
scala.collection.Seq                   | asJava              | java.util.List
scala.collection.mutable.Set           | asJava              | java.util.Set
scala.collection.Set                   | asJava              | java.util.Set
scala.collection.mutable.Map           | asJava              | java.util.Map
scala.collection.Map                   | asJava              | java.util.Map
scala.collection.mutable.Map           | asJavaDictionary    | java.util.Dictionary
scala.collection.mutable.ConcurrentMap | asJavaConcurrentMap | java.util.concurrent.ConcurrentMap
—————————————————————————————————————————————————————————————————————————————————————————————————
java.util.Iterator                     | asScala             | scala.collection.Iterator
java.util.Enumeration                  | asScala             | scala.collection.Iterator
java.lang.Iterable                     | asScala             | scala.collection.Iterable
java.util.Collection                   | asScala             | scala.collection.Iterable
java.util.List                         | asScala             | scala.collection.mutable.Buffer
java.util.Set                          | asScala             | scala.collection.mutable.Set
java.util.Map                          | asScala             | scala.collection.mutable.Map
java.util.concurrent.ConcurrentMap     | asScala             | scala.collection.mutable.ConcurrentMap
java.util.Dictionary                   | asScala             | scala.collection.mutable.Map
java.util.Properties                   | asScala             | scala.collection.mutable.Map[String, String]

To use the conversions directly from Java, though, you're better off calling methods from JavaConversions directly; e.g.:

List<String> javaList = new ArrayList<String>(Arrays.asList("a", "b", "c"));
System.out.println(javaList); // [a, b, c]
Buffer<String> scalaBuffer = JavaConversions.asScalaBuffer(javaList);
System.out.println(scalaBuffer); // Buffer(a, b, c)
List<String> javaListAgain = JavaConversions.bufferAsJavaList(scalaBuffer);
System.out.println(javaList == javaListAgain); // true
请叫√我孤独 2024-12-25 22:59:41

对于自 Scala 2.12.x 以来遇到此问题的任何人,JavaConversions 现已弃用,而 JavaConverters 是首选方法。

For anyone landing on this question since Scala 2.12.x, JavaConversions is now deprecated and JavaConverters is the preferred method.

乖乖哒 2024-12-25 22:59:41

在 Scala 2.13 中,JavaConverters弃用,取而代之的是 <代码>scala.jdk.CollectionConverters:

...带有对象 CollectionConverters 的新包 scala.jdk
(经典的Java集合,类似于collection.JavaConverters
2.12)、StreamConvertersFunctionConvertersOptionConverters...

In Scala 2.13, JavaConverters have been deprecated in favour of scala.jdk.CollectionConverters:

...new package scala.jdk with objects CollectionConverters
(classic Java collections, similar to collection.JavaConverters in
2.12), StreamConverters, FunctionConverters and OptionConverters...

椒妓 2024-12-25 22:59:41

如 API 中所述,JavaConversions 是一组隐式转换,将 java 集合转换为相关的 scala 集合。

您可以将其与 import collection.JavaConversions._ 一起使用。必要时,编译器会自动将java集合转换为正确的scala类型。

JavaConverters 是一组装饰器,可帮助使用 asScalaasJava 方法将 java 或 scala 集合转换为 scala 或 java 集合,这些方法将隐式添加到要转换的集合中。为了使用这些转换器,您需要导入:

import collection.JavaConverters._

您应该更喜欢 JavaConversions 因为它通常更容易使用(不需要使用 asScalaasJava >)。

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 using asScala or asJava methods that will be implicitly added to the collection that you want to transform. In order to use these converters, you need to import :

import collection.JavaConverters._

You should prefer JavaConversions as it's generally easier to use (no need to use asScala or asJava).

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