Converting a Java collection into a Scala collection
Scala interoperates with a lot of Java libraries, so you may have to deal with Java code as well. It's possible for you to encounter a situation where you've a Java collection and you're expected to convert it into Scala. With this idea of converting a collection from Java to Scala, it may look a little bit scary that we've got to traverse through elements from Java's collection, and then append them to a Scala collection. But here's the catch: there're already ways you can do this. It's easy: we've a JavaConverters
object in the scala.collection
package, which takes care of this conversion. Actually, what happens behind the scenes when you call these conversion methods is that the implicit conversion takes place. Why not take a look at an example:
package chapter5 import java.time.LocalDate import scala.collection.JavaConverters._ object CollectionConvertors extends App { /* We'll create a java collection of a couple of days...