I have a Java API that returns a List like:
public List> getByXPath(String xpathExpr)
I am using the below scala code:
If you have to convert a Java List
to a Scala List[ClassB]
, then you must do the following:
1) Add
import scala.collection.JavaConverters._
2) Use methods asScala
, toList
and then map
List javaList = ...
var scalaList[ClassB] = javaList.asScala.toList.map(x => new ClassB(x))
3) Add the following to the ClassB
constructor that receives ClassA
as a parameter:
case class ClassB () {
def this (classA: ClassA) {
this (new ClassB (classA.getAttr1, ..., classA.getAttrN))
}
}