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.
About the author
I'm currently working on real-time user-facing analytics with Apache Pinot at StarTree. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also I co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.