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.

- 43,673
- 4
- 57
- 93

- 81
- 2
- 3
-
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 Jun 24 '18 at 21:29
2 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();

- 43,673
- 4
- 57
- 93
-
Thank you for your answer! I tried to do that in Scala and it works! But one more question: Could you please tell me how to save a Scala `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] ` – Yiming Sun Jun 24 '18 at 23:16
-
1@YimingSun This looks like it should be a separate question tagged `[apache-spark]` and `[parquet]`. Maybe you can find some useful hints [here](https://stackoverflow.com/questions/36648128/how-to-store-custom-objects-in-dataset), but I cannot guarantee that there weren't any changes in the most recent versions. – Andrey Tyukin Jun 24 '18 at 23:23
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).

- 11
- 1