How to get Scala List from Java List?

前端 未结 6 1283
半阙折子戏
半阙折子戏 2021-01-29 23:23

I have a Java API that returns a List like:

public List getByXPath(String xpathExpr)

I am using the below scala code:

         


        
6条回答
  •  一个人的身影
    2021-01-29 23:51

    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.

提交回复
热议问题