是否将Akka Iterable转换为java.lang.Iterable?
我想在for-each循环中迭代给定参与者的子级,如下所示:是否将Akka Iterable转换为java.lang.Iterable?,java,scala,akka,Java,Scala,Akka,我想在for-each循环中迭代给定参与者的子级,如下所示: for(ActorRef child: this.getContext().children()){ // do things } 但这会产生一个错误: HelloWorld.java:78: error: for-each not applicable to expression type for(ActorRef child: this.getConte
for(ActorRef child: this.getContext().children()){
// do things
}
但这会产生一个错误:
HelloWorld.java:78: error: for-each not applicable to expression type
for(ActorRef child: this.getContext().children())
^
required: array or java.lang.Iterable
found: Iterable<ActorRef>
1 error
HelloWorld.java:78:error:for-each不适用于表达式类型
for(ActorRef子项:this.getContext().children())
^
必需:数组或java.lang.Iterable
found:Iterable假设children()
方法应该返回一个“Iterable[ActorRef]
”,但是该特定“Iterable
”的类型定义的内联超链接会导致Scala Iterable类型而不是Java类型的文档,这是不同的
这可以在实践中得到证实:从children()
调用返回的对象未通过“instanceOf Iterable”检查,并在其上调用“getClass()
”返回“classakka.actor.dungeon.ChildrenContainer$ChildrenIterable
”
我似乎很清楚,这是而不是Java Iterable,并且错误是适当的。但是,我如何将其强制或马歇尔化为一个Java Iterable?建议有Scala->Java的转换函数,但我无法确定导入什么或如何从Java调用它们;我见过的唯一例子是Scala
另外,我意识到我可能可以使用while循环和children().Iterator()
返回的Scala迭代器来为这里的每个循环构造a的等价物。我真正想要的是理解如何使用Scala提供的类型转换例程。我没有使用Scala,但是文档说
java.lang.Iterable<ActorRef> jChildren = JavaConversions.asJavaIterable (this.getContext().children());
for (ActorRef child: jChildren) {
// do stuff
}
我没有用Scala,但医生说
java.lang.Iterable<ActorRef> jChildren = JavaConversions.asJavaIterable (this.getContext().children());
for (ActorRef child: jChildren) {
// do stuff
}
如果您使用的是Java,那么您将使用UntypedActor及其UntypedActorContext,它提供了一个getChildren()
-方法:
文件:
java.lang.Iterable getChildren()
返回包含链接的参与者的不可修改的Java集合,请注意,支持映射是线程安全的,但不是不可变的
如果您使用的是Java,那么您将使用UntypedActor及其UntypedActorContext,它提供了一个getChildren()
-方法:
文件:
java.lang.Iterable getChildren()
返回包含链接的参与者的不可修改的Java集合,请注意,支持映射是线程安全的,但不是不可变的
这就把它清理干净了,谢谢!我会投票,但我还没有足够的业力去做那件事。@SAyotte如果你是问题的提问者,那么你就接受答案,而不是投票。这澄清了问题,谢谢!“我会投赞成票,但我还没有足够的因果报应来这么做。”萨约特如果你是问题的提问者,那么你就接受答案,而不是投赞成票。
java.lang.Iterable<ActorRef> getChildren()
Returns an unmodifiable Java Collection containing the linked actors, please note that the backing map is thread-safe but not immutable