从 scala.collection.Seq<String> 转换到 java.util.List<String>在 Java 代码中

我正在从 Java 调用 Scala 方法.我需要进行从 Seq 到 List 的转换.

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

我无法修改 Scala 方法的签名,因此无法使用 scala.collection.JavaConversions._

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?

使用 Scala 2.9.3

Using Scala 2.9.3

推荐答案

您使用 JavaConversions 走在正确的轨道上,但是此特定转换所需的方法是 seqAsJavaList:

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);
}

更新:JavaConversions 已弃用,但可以在 JavaConverters.

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);
}

相关文章