Skip to content

Add code tabs for collections-2.13/iterators, creating..., conversions... and options #2575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 4, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add code tabs for collections-2.13/conversions
  • Loading branch information
flomebul authored and bishabosha committed Oct 4, 2022
commit 73f782ab26fdb2521332fbc84763e702ef7b5c77
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Like Scala, Java also has a rich collections library. There are many similaritie

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 [CollectionConverters](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/jdk/CollectionConverters$.html) object. In particular, you will find bidirectional conversions between the following types.


Iterator <=> java.util.Iterator
Iterator <=> java.util.Enumeration
Iterable <=> java.lang.Iterable
Expand All @@ -28,11 +27,20 @@ Sometimes you might need to pass from one collection framework to the other. For

To enable these conversions, simply import them from the [CollectionConverters](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/jdk/CollectionConverters$.html) object:

{% tabs java_scala_1 %}
{% tab 'Scala 2 and 3' for=java_scala_1 %}

scala> import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters._

{% endtab %}
{% endtabs %}

This enables conversions between Scala collections and their corresponding Java collections by way of extension methods called `asScala` and `asJava`:

{% tabs java_scala_2 %}
{% tab 'Scala 2 and 3' for=java_scala_2 %}

scala> import collection.mutable._
import collection.mutable._

Expand All @@ -45,6 +53,9 @@ This enables conversions between Scala collections and their corresponding Java
scala> val m: java.util.Map[String, Int] = HashMap("abc" -> 1, "hello" -> 2).asJava
m: java.util.Map[String,Int] = {abc=1, hello=2}

{% endtab %}
{% endtabs %}

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.

Certain other Scala collections can also be converted to Java, but do not have a conversion back to the original Scala type:
Expand All @@ -56,9 +67,15 @@ Certain other Scala collections can also be converted to Java, but do not have a

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:

{% tabs java_scala_3 %}
{% tab 'Scala 2 and 3' for=java_scala_3 %}

scala> val jul = List(1, 2, 3).asJava
jul: java.util.List[Int] = [1, 2, 3]

scala> jul.add(7)
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)

{% endtab %}
{% endtabs %}