How to get Scala List from Java List?

JavaScala

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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._

Solution 2 - Java

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).asScala
lst.foreach{ node =>   .... }

This should give you Scala's Buffer representation allowing you to accomplish foreach.

Solution 3 - Java

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()

Solution 4 - Java

If you have to convert a Java List<ClassA> 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))    } }

Solution 5 - Java

Shortcut to convert java list to scala list

import scala.jdk.CollectionConverters._

myjavaList.asScala.toList

Solution 6 - Java

Since scala 2.8.1 use JavaConverters._ to convert scala and Java collections using asScala and asJava methods.

import scala.collection.JavaConverters._

javalist.asScala

scalaSeq.asJava

see the Conversion relationship scala doc site

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaceView Question on Stackoverflow
Solution 1 - JavaaceView Answer on Stackoverflow
Solution 2 - Javamontreal.ericView Answer on Stackoverflow
Solution 3 - JavaRadixView Answer on Stackoverflow
Solution 4 - JavaLeandro carrascoView Answer on Stackoverflow
Solution 5 - JavaXing-Wei LinView Answer on Stackoverflow
Solution 6 - Javauser1553728View Answer on Stackoverflow