la中的JavaConverters和JavaConversio

la中的JavaConverters和JavaConversio

本文介绍了Scala中的JavaConverters和JavaConversions有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

scala.collection 中,有两个非常相似的对象 JavaConversions 和.

  • 这两个对象有什么区别?
  • 为什么它们都存在?
  • 我何时要使用一个vs.另一个?

推荐答案

Java Conversions在Scala 2.13.0中获得了@deprecated.请改用 scala.jdk.CollectionConverters .

Java Conversions got @deprecated in Scala 2.13.0. Use scala.jdk.CollectionConverters instead.

JavaConversions提供了一系列隐式方法,这些方法在Java集合和最接近的对应Scala集合之间进行转换,反之亦然.这是通过创建实现Scala接口并将调用转发到基础Java集合或Java接口并将调用转发到基础Scala集合的包装器来完成的.

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使用pimp-my-library模式将asScala方法添加"到Java集合,将asJava方法添加"到Scala集合,这将返回上面讨论的适当包装器.它(从2.8.1版开始)比JavaConversions(从2.8版开始)更新,并使Scala和Java集合之间的转换变得明确.与David在他的答案中写的相反,我建议您养成使用JavaConverters的习惯,因为您写出进行大量隐式转换的代码的可能性要小得多,因为您可以控制唯一的位置会发生这种情况:在您编写.asScala.asJava的地方.

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.

这是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调用方法.例如:

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

这篇关于Scala中的JavaConverters和JavaConversions有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:57