Skip to content

Commit d4a3128

Browse files
committed
Merge pull request #509 from som-snytt/issue/9684-java-conversions
SI-9684 Don't push JavaConversions
2 parents 201547d + 4538ec0 commit d4a3128

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

overviews/collections/conversions-between-java-and-scala-collections.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ languages: [ja, zh-cn]
1111

1212
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.
1313

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.
1515

1616

1717
Iterator <=> java.util.Iterator
@@ -23,25 +23,28 @@ Sometimes you might need to pass from one collection framework to the other. For
2323
mutable.Map <=> java.util.Map
2424
mutable.ConcurrentMap <=> java.util.concurrent.ConcurrentMap
2525

26-
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:
2727

28-
scala> import collection.JavaConversions._
29-
import collection.JavaConversions._
28+
scala> import collection.JavaConverters._
29+
import collection.JavaConverters._
3030

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`:
3232

3333
scala> import collection.mutable._
3434
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
3637
jul: java.util.List[Int] = [1, 2, 3]
37-
scala> val buf: Seq[Int] = jul
38+
39+
scala> val buf: Seq[Int] = jul.asScala
3840
buf: scala.collection.mutable.Seq[Int] = ArrayBuffer(1, 2, 3)
39-
scala> val m: java.util.Map[String, Int] = HashMap("abc" -> 1, "hello" -> 2)
40-
m: java.util.Map[String,Int] = {hello=2, abc=1}
41+
42+
scala> val m: java.util.Map[String, Int] = HashMap("abc" -> 1, "hello" -> 2).asJava
43+
m: java.util.Map[String,Int] = {abc=1, hello=2}
4144

4245
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.
4346

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:
4548

4649
Seq => java.util.List
4750
mutable.Seq => java.util.List
@@ -50,9 +53,10 @@ The are some other common Scala collections than can also be converted to Java t
5053

5154
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:
5255

53-
scala> jul = List(1, 2, 3)
56+
scala> val jul = List(1, 2, 3).asJava
5457
jul: java.util.List[Int] = [1, 2, 3]
58+
5559
scala> jul.add(7)
5660
java.lang.UnsupportedOperationException
57-
at java.util.AbstractList.add(AbstractList.java:131)
61+
at java.util.AbstractList.add(AbstractList.java:148)
5862

0 commit comments

Comments
 (0)