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

前端 未结 4 877
Happy的楠姐
Happy的楠姐 2020-12-09 08:53

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

4条回答
  •  抹茶落季
    2020-12-09 09:20

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

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

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

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

提交回复
热议问题