![](/img/trans.png)
[英]How can I fix mismatch error in scala where the found : Seq[scala.collection.immutable.Seq required: scala.collection.Seq?
[英]How to create a scala.collection.immutable.Seq from a Java List in Java?
我試圖讓 Akka 進入我的 Java 項目,但我對來自 Scala 的 Seq 類型的一個小問題感到困惑。 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. 我怎樣才能做一個?
代碼:
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;
}
}
You can use scala.collection.JavaConversions.asScalaBuffer
to convert the Java List
to a Scala Buffer
, which has a toList
method, and a Scala List
is a collection.immutable.Seq
.
路由器的 akka Java 文檔以及 CyclicIterator 的ScalaDoc都建議 CyclicIterator 構造函數采用 List。
您可以使用:
scala.collection.JavaConverters.collectionAsScalaIterableConverter(workers).asScala().toSeq()
你可以試試這個:
scala.collection.JavaConverters.asScalaIteratorConverter(list.iterator()).asScala().toSeq();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.