Converting collections to and fro in Scala / Java

Say that you have to use a Java library in your Scala application and this Java library has a couple of API's that require you to pass a Java collection type. Let's see how we can cope with it by looking at the following examples:

Suppose I have a Java ArrayList that contains some Integers that I want to map these List of Integers by incrementing 1 to each of the elements in the ArrayList as below:

 1scala> val javaList = new java.util.ArrayList[Integer]()
 2javaList: java.util.ArrayList[Integer] = []
 3
 4scala> javaList.add(1)
 5res0: Boolean = true
 6
 7scala> javaList.add(2)
 8res1: Boolean = true
 9
10scala> javaList map { i => i + 1 }
11error: value map is not a member of java.util.ArrayList[Integer]
12    javaList map { i => i + 1 }

As you can see that the message from the Scala compiler is very clear that there is no map function available to the ArrayList that we need someway to tell the compiler to treat that as a Scala collection. Java Conversions to the rescue:

1import scala.collection.JavaConversions._
2
3scala> javaList map { i => i + 1 }
4res3: scala.collection.mutable.Buffer[Int] = ArrayBuffer(2, 3)

What happened above is that we imported the JavaConversions package available in the Scala library to tell the compiler that it should implicitly convert the java.util.ArrayList to a closest possible collection type in Scala which in our case happened to be an ArrayBuffer. If you take a look at the Scala's API documentation on JavaConversions there is an implicit method called asScalaBuffer which was used in our example implicitly to convert the java.util.ArrayList to a Scala collection type.

The JavaConversions package contains a couple of implicit methods to convert between a Java collection to a Scala collection and vice-versa. There is yet another package in the scala.collections called JavaConverters. What is the difference? Both the JavaConversions and JavaConverters do more or less the same thing, i.e., convert to and fro between a Java / Scala collection. You might be wondering over which one you should prefer. Let's find that out with the help of some more examples:

1scala> val scalaMap = Map("A" -> 1)
2scalaMap: scala.collection.immutable.Map[String,Int] = Map(A -> 1)
3
4scala> scalaMap.contains(1)
5error: type mismatch;
6found   : Int(1)
7required: String
8scalaMap.contains(1)
9^

What we did just now is instantiated an immutable Map from the Scala library that maps a set of String to Int. Remember, the syntax for Scala Map looks like below:

1trait Map[A, +B] extends Iterable[(A, B)] with GenMap[A, B]
2    with MapLike[A, B, Map[A, B]]

The contains method looks like below:

1def contains(key: A): Boolean

What we did with our scalaMap variable above is that we created a Map that took a String as key and Int as value, so when we called the contains method and passed it an Int, it failed. The contains method implementation in Scala internally calls the get method to check if the key is contained in the collection. Now let's see what happens if we import the JavaConversions:

1scala> import scala.collection.JavaConversions._
2import scala.collection.JavaConversions._
3
4scala> scalaMap.contains(1)
5res1: Boolean = false

It kind of worked and returned false. Practically speaking the variable scalaMap does not contain 1, so returning false would look like the right thing to do. What was compromised here is the type safety that we had before importing the JavaConversions. Soon after importing the JavaConversions package, the Scala compiler gives an implicit call to the MapWrapper class wherein the MapWrapper class implements the containsKey method from the Java library. The implementation looks like this in the Scala library's MapWrapper class

1override def containsKey(key: AnyRef): Boolean = try {
2// Note: Subclass of collection.Map with specific key type may redirect generic
3// contains to specific contains, which will throw a ClassCastException if the
4// wrong type is passed. This is why we need a type cast to A inside a try/catch.
5underlying.contains(key.asInstanceOf[A])
6} catch {
7    case ex: ClassCastException => false
8}

As it can be seen, the containsKey takes an Object / AnyRef rather than the specific type which in our case is a String. With JavaConversions, we went down from a stricter type to a more non stricter type. Let's see now how we could retain the stricter type by being a bit more explicit, using the JavaConverters!

 1scala> val scalaMap = Map("A" -> 1)
 2scalaMap: scala.collection.immutable.Map[String,Int] = Map(A -> 1)
 3
 4scala> import scala.collection.JavaConverters._
 5import scala.collection.JavaConverters._
 6
 7scala> scalaMap.contains(1)
 8error: type mismatch;
 9found   : Int(1)
10required: String
11scalaMap.contains(1)
12^

Did you see the difference? There is now no implicit conversion between the Java and Scala collection types. If you want the scalaMap variable to be a java.util.Map, you now have to be explicit as below:

1scala> scalaMap.asJava
2res0: java.util.Map[String,Int] = {A=1}

So from my point of view, it is better to be explicit which would imply to use the JavaConverters instead of JavaConversions.