Scala: Converting a scala collection to java.util.List
I’ve been playing around a little with Goose - a library for extracting the main body of text from web pages - and I thought I’d try converting some of the code to be more scala-esque in style.
The API of the various classes/methods is designed so it’s interoperable with Java code but in order to use functions like map/filter we need the collection to be a Scala one.
That’s achieved by importing ‘scala.collections.JavaConversions._’ which will apply an implicit conversion to convert the Java collection into a Scala one.
I needed to go back to the Java one again which can be achieved with the following code:
import scala.collection.JavaConversions._ val javaCollection = seqAsJavaList(Seq("abc"))
I also used that function in the StopWords.scala object in Goose.
There are a load of other functions available in JavaConversions as well for going to a Dictionary, Map, Set and so on.