You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: overviews/collections/conversions-between-java-and-scala-collections.md
+16-12Lines changed: 16 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ languages: [ja, zh-cn]
11
11
12
12
Like Scala, Java also has a rich collections library. There are many similarities between the two. For instance, both libraries know iterators, iterables, sets, maps, and sequences. But there are also important differences. In particular, the Scala libraries put much more emphasis on immutable collections, and provide many more operations that transform a collection into a new one.
13
13
14
-
Sometimes you might need to pass from one collection framework to the other. For instance, you might want to access to an existing Java collection, as if it was a Scala collection. Or you might want to pass one of Scala's collections to a Java method that expects its Java counterpart. It is quite easy to do this, because Scala offers implicit conversions between all the major collection types in the [JavaConversions](http://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/JavaConversions$.html) object. In particular, you will find bidirectional conversions between the following types.
14
+
Sometimes you might need to pass from one collection framework to the other. For instance, you might want to access an existing Java collection as if it were a Scala collection. Or you might want to pass one of Scala's collections to a Java method that expects its Java counterpart. It is quite easy to do this, because Scala offers implicit conversions between all the major collection types in the [JavaConverters](http://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/JavaConverters$.html) object. In particular, you will find bidirectional conversions between the following types.
15
15
16
16
17
17
Iterator <=> java.util.Iterator
@@ -23,25 +23,28 @@ Sometimes you might need to pass from one collection framework to the other. For
To enable these conversions, simply import them from the [JavaConversions](http://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/JavaConversions$.html) object:
26
+
To enable these conversions, simply import them from the [JavaConverters](http://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/JavaConverters$.html) object:
27
27
28
-
scala> import collection.JavaConversions._
29
-
import collection.JavaConversions._
28
+
scala> import collection.JavaConverters._
29
+
import collection.JavaConverters._
30
30
31
-
You have now automatic conversions between Scala collections and their corresponding Java collections.
31
+
This enables conversions between Scala collections and their corresponding Java collections by way of extension methods called `asScala` and `asJava`:
32
32
33
33
scala> import collection.mutable._
34
34
import collection.mutable._
35
-
scala> val jul: java.util.List[Int] = ArrayBuffer(1, 2, 3)
35
+
36
+
scala> val jul: java.util.List[Int] = ArrayBuffer(1, 2, 3).asJava
Internally, these conversion work by setting up a "wrapper" object that forwards all operations to the underlying collection object. So collections are never copied when converting between Java and Scala. An interesting property is that if you do a round-trip conversion from, say a Java type to its corresponding Scala type, and back to the same Java type, you end up with the identical collection object you have started with.
43
46
44
-
The are some other common Scala collections than can also be converted to Java types, but which to not have a corresponding conversion in the other sense. These are:
47
+
Certain other Scala collections can also be converted to Java, but do not have a conversion back to the original Scala type:
45
48
46
49
Seq => java.util.List
47
50
mutable.Seq => java.util.List
@@ -50,9 +53,10 @@ The are some other common Scala collections than can also be converted to Java t
50
53
51
54
Because Java does not distinguish between mutable and immutable collections in their type, a conversion from, say, `scala.immutable.List` will yield a `java.util.List`, where all mutation operations throw an "UnsupportedOperationException". Here's an example:
52
55
53
-
scala> jul = List(1, 2, 3)
56
+
scala> val jul = List(1, 2, 3).asJava
54
57
jul: java.util.List[Int] = [1, 2, 3]
58
+
55
59
scala> jul.add(7)
56
60
java.lang.UnsupportedOperationException
57
-
at java.util.AbstractList.add(AbstractList.java:131)
61
+
at java.util.AbstractList.add(AbstractList.java:148)
0 commit comments