問題描述
我正在從 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);
}
這篇關于從 scala.collection.Seq<String> 轉換到 java.util.List<String>在 Java 代碼中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!