[英]Conversion from scala[Seq] to java
我有一个scala case类,看起来像这样:
case class AddressView(id: Option[Long],
address: Address,
purpose: Seq[String])
我必须从Java调用此类。
这似乎不起作用:
AddressView billToAddress = new AddressView
(BusinessFieldValue.ShipToAddressId,
shipAddress,
(Seq<String>)Arrays.asList("BILLING"));
谁能告诉我正确的方法?
因此,您需要修复两件事。 您需要将Long封装在Option中,并且需要将List正确转换为Scala Seq。
您的addressview类采用Option [Long]作为参数。 如果您可以将long传递给构造函数,那将是很好的选择,但是必须将long包装在Option中。
Option有2个子类,“一些”和“无”。 因此,您需要将Some导入到Java程序中。 您还必须将scala库添加到类路径中。
对于转换,scala在scala.collection.JavaConversions中提供了一些内置功能。
所以最终结果是
import scala.Some;
import scala.collection.JavaConversions;
import java.util.Arrays;
public class Test {
public static void main(String[] args){
AddressView billToAddress = new AddressView
(new Some<>(BusinessFieldValue.ShipToAddressId),
shipAddress,
JavaConversions.asScalaBuffer(Arrays.asList("BILLING")));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.