Convert from scala.collection.Seq<String> to java.util.List<String> in Java code

Cacho Santa picture Cacho Santa · Jul 19, 2013 · Viewed 23.4k times · Source

I'm calling a Scala method, from Java. And I need to make the conversion from Seq to List.

I can't modified the signature of the Scala method, so I can't used the asJavaCollection method from scala.collection.JavaConversions._

Any ideas of how can I achieve this?

Using Scala 2.9.3

Answer

Chris Martin picture Chris Martin · Jul 19, 2013

You're on the right track using JavaConversions, but the method you need for this particular conversion is seqAsJavaList:

java.util.List<String> convert(scala.collection.Seq<String> seq) {
    return scala.collection.JavaConversions.seqAsJavaList(seq);
}

Update: JavaConversions is deprecated, but the same function can be found in JavaConverters.

java.util.List<String> convert(scala.collection.Seq<String> seq) {
    return scala.collection.JavaConverters.seqAsJavaList(seq);
}