How to get Scala List from Java List?
EDIT: Note that this is deprecated since 2.12.0. Use JavaConverters
instead. (comment by @Yaroslav)
Since Scala 2.8 this conversion is now built into the language using:
import scala.collection.JavaConversions._...lst.toList.foreach{ node => .... }
works. asScala
did not work
In 2.12.x use import scala.collection.JavaConverters._
In 2.13.x use import scala.jdk.CollectionConverters._
There's a handy Scala object just for this - scala.collection.JavaConverters
You can do the import and asScala
afterwards as follows:
import scala.collection.JavaConverters._val lst = node.getByXPath(xpath).asScalalst.foreach{ node => .... }
This should give you Scala's Buffer
representation allowing you to accomplish foreach
.
I was looking for an answer written in Java and surprisingly couldn't find any clean solutions here. After a while I was able to figure it out so I decided to add it here in case someone else is looking for the Java implementation (I guess it also works in Scala?):
JavaConversions.asScalaBuffer(myJavaList).toList()