Using scala object inside java?

In my java code, I am calling a method from a class that is defined in Scala and I want to use one of its methods in java. This is what I call it and it works great.

Seq<SomeObjectType> variableName = ScalaClass.MethodInTheScalaClass(); 

      

I can call this function in java on this form, but since I am calling this method from the compiled package I cannot see it happening (and therefore I cannot change it).

The problem is I don't know how to iterate over "variableName" in java (since Seq is a scala type).

How can I iterate over a variable name or convert it to a Java object (like List)?

+3


source to share


1 answer


Try the following:

java.util.List<SomeObjectType> res = 
  scala.collection.JavaConverters$.MODULE$.seqAsJavaListConverter(variableName).asJava();

      



You can get a list of converters in the JavaConverters documentation .

You must use JavaConverters$.MODULE$

to get an object JavaConverters

from Java

.

+3


source







All Articles