In Java, I already have a scala.collection.mutable.wrappedArray object. How can I convert it to util.ArrayList or util.linkedList in Java? Or alternatively, How can I iterate it and do something with each value of it? foreach is not applicable.
-
You want to iterate over it but you can't use a for-each loop? Why is that? For-each is the most straightforward, not to mention general, iteration tool at your disposal.Silvio Mayolo– Silvio Mayolo2018-06-24 21:29:56 +00:00Commented Jun 24, 2018 at 21:29
3 Answers
I don't want to claim that it's "impossible" to use a scala.collection.mutable.wrappedArray from your Java code, but in my opinion, if you are asking such questions, you're doing it wrong.
Scala's standard library does provide a lot of functionality to convert between Scala data structures and Java data structures.
Java standard library does not provide any tools to deal with Scala collections, it does not know anything at all about Scala.
Therefore, the question "how can I convert scala.x.y.Z to java.r.s.T in Java" is essentially meaningless: don't try to perform the conversion in Java, do it in Scala, it's much easier.
General approach
Suppose that you have a method foo implemented in Scala that returns a Scala-specific data structure scala.x.y.Z:
// Scala code
def foo(): scala.x.y.Z = ???
Apparently, what you are trying to do is:
// Java code
scala.x.y.Z a = yourScalaLibrary.foo();
java.r.s.T b = /*
crazily complex java code
that converts scaa.x.y.Z to java.r.s.T
*/
My suggestion is: just don't do it. Instead, implement an additional Java-specific API in Scala:
// Scala code
def javaFoo(): java.r.s.T = {
scala.x.y.Z a = foo()
java.r.s.T b = /* simple conversion code using Scala-Java interop */
b // return Java-specific collection
}
And then use only the Java-API in your Java code:
// Java code
java.r.s.T b = javaFoo();
Concrete example
Assuming that you want to use this method from Java:
def foo(): scala.collection.mutable.WrappedArray[Int] = Array(1, 2, 3)
you first implement a java-API (in Scala!):
import collection.JavaConverters._
def javaFoo(): java.util.List[Int] = foo().asJava
and then use it from Java:
java.util.List myList = javaFoo();
2 Comments
DataFrame object with parquet format with each row contains a, say, java.util.ArrayList object? Or any object the class of which implements List interface? Directly save it will leading to the following error: ` Exception in thread "main" java.lang.UnsupportedOperationException: No Encoder found for java.util.ArrayList[Double] `[apache-spark] and [parquet]. Maybe you can find some useful hints here, but I cannot guarantee that there weren't any changes in the most recent versions.This is what I ended up doing (in Java):
import scala.collection.JavaConverters;
/* ... */
JavaConverters.mutableSeqAsJavaList(yourArray.seq());
As far as I know, scala.* packages naturally get into classpath once you have any library that uses Scala (and specifically the one you want to convert the data from).