How to get Scala List from Java List?

Viewed 99030

I have a Java API that returns a List like:

public List<?> getByXPath(String xpathExpr)

I am using the below scala code:

val lst = node.getByXPath(xpath)

Now if I try scala syntax sugar like:

lst.foreach{ node => ... }

it does not work. I get the error:

value foreach is not a member of java.util.List[?0]

It seems I need to convert Java List to Scala List. How to do that in above context?

6 Answers

Shortcut to convert java list to scala list

import scala.jdk.CollectionConverters._

myjavaList.asScala.toList

Related