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

前端 未结 4 879
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:21

    Since Scala 2.9, you shouldn't use implicits from JavaConversions since they are deprecated and will soon be removed. Instead, to convert Seq into java List use convert package like this (although it doesn't look very nice):

    import scala.collection.convert.WrapAsJava$;
    
    public class Test {
        java.util.List convert(scala.collection.Seq seq) {
            return WrapAsJava$.MODULE$.seqAsJavaList(seq);
        }
    }
    

提交回复
热议问题