如何在 Java 中从 Java 列表创建 scala.collection.immutable.Seq?
我正在尝试让 Akka 在我的 Java 项目中运行,但我被 Scala 中 Seq 类型的一个小问题所困扰。我能够将 ActorRef 的 Java 列表转换为 scala.collection.Seq,但我尝试使用的 Akka API 需要 scala.collection.immutable.Seq。我怎样才能制作一个?
代码:
static class Router extends UntypedLoadBalancer {
private final InfiniteIterator<ActorRef> workers;
public Router(List<ActorRef> workers) {
Seq workerSeq = asScalaBuffer(workers);
// how to get from the scala.collection.Seq above to the instance of
// scala.collection.immutable.Seq required by CyclicIterator below?
this.workers = new CyclicIterator<ActorRef>();
}
public InfiniteIterator<ActorRef> seq() {
return workers;
}
}
I'm trying to get Akka going in my Java project, and I'm hung up on a small issue with the Seq type(s) from Scala. I'm able to convert my Java List of ActorRef into a scala.collection.Seq, but the Akka API I'm trying to use requires a scala.collection.immutable.Seq. How can I make one?
Code:
static class Router extends UntypedLoadBalancer {
private final InfiniteIterator<ActorRef> workers;
public Router(List<ActorRef> workers) {
Seq workerSeq = asScalaBuffer(workers);
// how to get from the scala.collection.Seq above to the instance of
// scala.collection.immutable.Seq required by CyclicIterator below?
this.workers = new CyclicIterator<ActorRef>();
}
public InfiniteIterator<ActorRef> seq() {
return workers;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 scala.collection.JavaConversions.asScalaBuffer 来将 Java List 转换为 Scala Buffer,后者具有 toList code> 方法,Scala
List
是一个collection.immutable.Seq
。You can use
scala.collection.JavaConversions.asScalaBuffer
to convert the JavaList
to a ScalaBuffer
, which has atoList
method, and a ScalaList
is acollection.immutable.Seq
.akka 路由器 Java 文档 以及 ScalaDoc for < a href="http://akka.io/api/akka/1.1.3/#akka.routing.CyclicIterator" rel="nofollow">CyclicIterator 都表明CyclicIterator 构造函数采用一个 List。
The akka Java documentation for routers as well as the ScalaDoc for CyclicIterator both suggest that the CyclicIterator constructor takes a List.
你可以试试这个:
You can try this:
您可以使用:
scala.collection.JavaConverters.collectionAsScalaIterableConverter(workers).asScala().toSeq()
You can use:
scala.collection.JavaConverters.collectionAsScalaIterableConverter(workers).asScala().toSeq()
由于
scala
2.12.0
JavaConversions
已被标记为已弃用,因此应该使用
JavaConverters
或惯用的方式
Since
scala
2.12.0
JavaConversions
is marked deprecatedso one should use
JavaConverters
or idiomatically