score:185
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._
score:6
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
score:9
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 <ClassA> 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))
}
}
score:9
Shortcut to convert java list to scala list
import scala.jdk.CollectionConverters._
myjavaList.asScala.toList
score:10
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()
score:98
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
.
Source: stackoverflow.com
Related Query
- How to get Scala List from Java List?
- How to pass Java List to Scala, filter it in Scala and get back to Java?
- How to get column values from list which contains column names in spark scala dataframe
- get java object array from scala list
- How to get list of all leaf folders from ADLS Gen2 path via Scala code?
- How to get the class of a trait within an object defined in scala from within a java class?
- How to get distinct values from an element within list of tuples in Scala
- How to get Common list from two list in scala
- How to get a List of Maps from a List of Objects in Scala
- How to get unique key values from List of maps in Scala
- How to get list of json strings from sinlgle line multi json string in scala
- how to get value from list based on condition in scala
- How do I get the Scala version from within Scala itself?
- In Scala, how to get a slice of a list from nth element to the end of the list without knowing the length?
- How to get methods list in scala
- How to create a scala.collection.immutable.Seq from a Java List in Java?
- How to show scala doc from Java Editor in Eclipse?
- Scala 2.10 reflection, how do I extract the field values from a case class, i.e. field list from case class
- How to remove an item from a list in Scala having only its index?
- Get head item and tail items from scala list
- How to get a random element from a Set in Scala
- How to use Scala varargs from Java code
- How to randomly sample from a Scala list or array?
- How can I idiomatically "remove" a single element from a list in Scala and close the gap?
- How to call main method of a Scala program from the main method of a java program?
- How to create a Row from a List or Array in Spark using Scala
- How to access a Java static method from Scala given a type alias for that class it resides in
- How to refer to protected inner class in Scala when inheriting from Java (with byte code only)
- How to correctly get current loop count from a Iterator in scala
- How to get distinct items from a Scala Iterable, maintaining laziness
More Query from same tag
- How to emit tuples instead of a list of tuples
- Scala/Java parse date unknown format
- Akka persistence with confirmed delivery gives inconsistent results
- Using Undercover with ScalaTest and Maven
- Can not run project with maven configuration
- How to calculate the average of rows between a start index and end index of a column in a dataframe in Spark using Scala?
- Why doesn't Scala allow one to instantiate a class without using the "new" operator?
- Building a jar to run on Linux Server
- How can I scala-ify this block:
- Dynamically loading a Scala object
- How to load newer version of json4s in Spark 2.1 executor?
- Convert legacy Java code into Scala functional idioms
- Converting one case class to another that is similar with additional parameter in Scala
- Akka: Trying to understand order of message delivery between Actors
- A case when Slick uses more connections than threads
- Scala: function as a parameter to map()
- How to use DATABSE_URL in Play 2.0 (Scala) for local connection to mysql
- Create a Seq of Elements based on Increment in Scala
- Scala - error when passing parameters
- How to split String by "|" (pipe) and convert RDD to Dataframe
- How to combine Futures of different types into a single Future without using zip()
- Scala: Spark: java.lang.ClassNotFoundException:
- Create a Map from the elements of the same set based on condition in Scala
- Idiomatic (functional) file processing pipeline in Scala
- Working IntelliJ Scala Plugin Tutorial?
- Akka.io: Thread per Actor
- How does Scala define a type of return value?
- Read a CSV file with , as delim and numeric data also contain , separator to create RDD in Spark using Scala
- Update Mapping Defination is ELasticSearch using Scala
- Apache Spark - Generate List Of Pairs