[英]Convert Akka Iterable to java.lang.Iterable?
I would like to iterate over the children of a given Actor in a for-each loop, like so: 我想在for-each循环中遍历给定Actor的子代,如下所示:
for(ActorRef child: this.getContext().children()){
// do things
}
This yields an error though: 但这会产生一个错误:
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
The docs for UntypedActorContext say that the children()
method should return an ' Iterable[ActorRef]
', but the inline-hyperlink for the type definition for that particular ' Iterable
' leads to the docs for the Scala Iterable-type rather than the Java type, which are not the same thing. UntypedActorContext的文档说,
children()
方法应该返回一个' Iterable[ActorRef]
',但是该特定' Iterable
'的类型定义的内联超链接导致Scala Iterable-type而不是Java的文档。类型,这不是同一件事。
This can be confirmed in practice: the object returned from the children()
call fails an "instanceOf Iterable" check, and calling " getClass()
" on it returns "class akka.actor.dungeon.ChildrenContainer$ChildrenIterable
". 这可以在实践中得到确认:从
children()
调用返回的对象未通过“ instanceOf Iterable”检查,而对其调用“ getClass()
”则返回“ class akka.actor.dungeon.ChildrenContainer$ChildrenIterable
”。
It seems pretty clear to me that this is not a Java Iterable and that the error is appropriate. 在我看来,这显然不是 Java Iterable,并且该错误是适当的。 But how do I coerce or marshall it into a Java Iterable?
但是,如何将其强制或编组为Java Iterable? This link in the Scala docs suggests that there are conversion functions for Scala->Java, but I cannot make heads or tails of what to import or how to call them from Java;
Scala文档中的此链接表明存在Scala-> Java的转换函数,但是我无法确定要导入的内容或如何从Java调用它们的方法。 the only examples I've seen have been for Scala.
我见过的唯一例子就是Scala。
PS I realize I can probably use a while-loop and the Scala-Iterator returned by children().iterator()
to construct the equivalent of a for-each loop here. PS我意识到我可能可以使用while循环和
children().iterator()
返回的Scala-Iterator在这里构造一个for-each循环的等效项。 What I'm really after is understanding how to use the type-conversion routines that Scala provides. 我真正要了解的是如何使用Scala提供的类型转换例程。
I haven't used Scala, but the doc is saying 我没有用过Scala,但是医生说
java.lang.Iterable<ActorRef> jChildren = JavaConversions.asJavaIterable (this.getContext().children());
for (ActorRef child: jChildren) {
// do stuff
}
Note: if both types of Iterables are used in the same class you need to expand java.lang.Iterable in the declaration to make sure it uses the right one. 注意:如果两种Iterable都在同一类中使用,则需要在声明中扩展java.lang.Iterable,以确保使用正确的Iterable。
Alternately you could do in one line as such: 另外,您也可以这样一行:
for (ActorRef child: JavaConversions.asJavaIterable(this.getContext().children())) {
// do stuff
}
If you're using Java, you would be using UntypedActor and its UntypedActorContext, which provides a getChildren()
-method: http://doc.akka.io/japi/akka/2.3.4/akka/actor/UntypedActorContext.html 如果您使用的是Java,则将使用UntypedActor及其UntypedActorContext,它提供了
getChildren()
方法: http ://doc.akka.io/japi/akka/2.3.4/akka/actor/UntypedActorContext.html
Doc: 文件:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.