Convert Java List to Scala Seq
JavaConverters is what I needed to solve this.
import scala.collection.JavaConverters;
public Seq<String> convertListToSeq(List<String> inputList) {
return JavaConverters.asScalaIteratorConverter(inputList.iterator()).asScala().toSeq();
}
This worked for me! (Java 8, Spark 2.0.0)
import java.util.ArrayList;
import scala.collection.JavaConverters;
import scala.collection.Seq;
public class Java2Scala
{
public Seq<String> getSeqString(ArrayList<String> list)
{
return JavaConverters.asScalaIterableConverter(list).asScala().toSeq();
}
}
JavaConversions
should work. I think, you are looking for something like this: JavaConversions.asScalaBuffer(a).toSeq()
Starting Scala 2.13
, package scala.jdk.javaapi.CollectionConverters
replaces deprecated packages scala.collection.JavaConverters/JavaConversions
:
import scala.jdk.javaapi.CollectionConverters;
// List<String> javaList = Arrays.asList("a", "b");
CollectionConverters.asScala(javaList).toSeq();
// Seq[String] = List(a, b)