Daniel
Daniel

Reputation: 6039

Using scala object inside java?

In my java code, I am calling a method, from a class which is defined in Scala, and I want to use one of its methods in java. Here is how I call it and it works fine.

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

I can call this function in java in this form, but since I am calling this method from a compiled package, I can't see what it going on (and therefore I can't change it).

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

How can I iterate over variableName or convert it to a Java object (e.g. List)?

Upvotes: 3

Views: 408

Answers (1)

senia
senia

Reputation: 38045

Try this:

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

You could get converters list in JavaConverters documentation.

You should use JavaConverters$.MODULE$ to get JavaConverters object from Java.

Upvotes: 3

Related Questions