Java 中可以合并迭代器吗?
Java 中可以合并迭代器吗?我有两个迭代器,我想组合/合并它们,以便我可以一次性(在同一循环中)迭代它们的元素,而不是两步。这可能吗?
请注意,两个列表中的元素数量可能不同,因此对两个列表进行一次循环并不是解决方案。
Iterator<User> pUsers = userService.getPrimaryUsersInGroup(group.getId());
Iterator<User> sUsers = userService.getSecondaryUsersInGroup(group.getId());
while(pUsers.hasNext()) {
User user = pUsers.next();
.....
}
while(sUsers.hasNext()) {
User user = sUsers.next();
.....
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
Guava(以前称为 Google Collections)拥有 Iterators.concat 。
Guava (formerly Google Collections) has Iterators.concat.
此外,Apache Commons Collection 有几个用于操作迭代器的类,例如 IteratorChain,包装一个数字迭代器。
Also the Apache Commons Collection have several classes for manipulating Iterators, like the IteratorChain, that wraps a number of Iterators.
您可以创建自己的
Iterator
接口实现,该接口对迭代器进行迭代:(为了简洁起见,我没有向迭代器添加泛型。) 实现并不太难,但这并不是最简单的,您需要跟踪当前正在迭代的
Iterator
,并调用您需要迭代的next()
您可以遍历迭代器,直到找到返回true
的hasNext()
,否则您可能会到达最后一个迭代器的末尾。我不知道已有任何实现。更新:
我对 Andrew Duffy 的投了赞成票答案——不需要重新发明轮子。我真的需要更深入地研究番石榴。
我为可变数量的参数添加了另一个构造函数 - 几乎偏离了主题,因为这里的类如何构造并不是真正令人感兴趣,只是它如何工作的概念。
You could create your own implementation of the
Iterator
interface which iterates over the iterators:(I've not added generics to the Iterator for brevity.) The implementation is not too hard, but isn't the most trivial, you need to keep track of which
Iterator
you are currently iterating over, and callingnext()
you'll need to iterate as far as you can through the iterators until you find ahasNext()
that returnstrue
, or you may hit the end of the last iterator.I'm not aware of any implementation that already exists for this.Update:
I've up-voted Andrew Duffy's answer - no need to re-invent the wheel. I really need to look into Guava in more depth.
I've added another constructor for a variable number of arguments - almost getting off topic, as how the class is constructed here isn't really of interest, just the concept of how it works.
我已经有一段时间没有编写 Java 代码了,这让我很好奇我是否仍然“掌握了它”。
第一次尝试:
您当然可以使用更多的 Collection 类,而不是纯数组 + 索引计数器,但这实际上感觉比替代方案更干净。或者我现在只是对主要使用 C 语言有偏见?
无论如何,就这样吧。你的问题的答案是“是的,可能”。
I haven't written Java code in a while, and this got me curious to whether I've still "got it".
First try:
You could of course use more Collection classes rather than a pure array + index counter, but this actually feels a bit cleaner than the alternative. Or am I just biased from writing mostly C these days?
Anyway, there you go. The answer to you question is "yes, probably".
从 Java 8 及更高版本开始,可以使用 流 API。这还允许将迭代器与其他类型的流串联。
Starting with Java 8 and later this can be done without external dependencies using Stream API. This also allows concatenation of iterator with other types of streams.
将循环移至方法并将迭代器传递给方法。
move your loop to a method and pass the iterator to method.
迭代器来自集合或集合。
为什么不使用已有的方法
Collection.addAll(Collection c);
然后从最后一个对象创建迭代器。
这样,您的迭代器将迭代两个集合的所有内容。
an iterator comes FROM a collection or a set.
why not use the method already available
Collection.addAll(Collection c);
and then create your iterator from the last object.
this way, your iterator will iterate all the contents of both collection.
您可以使用我的版本的可扩展迭代器。它使用迭代器的双端队列,这对我来说是有意义的:
You can use my version of an extendable iterator. It uses a double-ended queue of iterators which to me makes sense:
合并迭代器:
创建
Iterable
的concat
方法:简单的 JUnit 测试:
The Merged Iterator:
The
concat
method to create anIterable
:Simple JUnit test:
我会将原始设计从: 重构
为:
I would refactor the original design from:
To something like:
您可以尝试
ConcatIterator
来自 Cactoos:另请检查
ConcatIterable
,连接Iterable
。You can try
ConcatIterator
from Cactoos:Also check
ConcatIterable
, which concatenatesIterable
s.在 Apache Commons Collections 中有
public static迭代器 org.apache.commons.collections4.IteratorUtils.chainedIterator(Collection> iterators)
上面写着这应该是你想要的。
In the Apache Commons Collections there is
public static <E> Iterator<E> org.apache.commons.collections4.IteratorUtils.chainedIterator(Collection<Iterator<? extends E>> iterators)
that sayswhich should be what you want.
每个
Iterator
对象都拥有自己的内存位置(地址),因此您不能简单地“合并”它们。除非您扩展迭代器类并在那里编写自己的实现。如果您在两个迭代器中处理相同数量的对象,另一种解决方案是在一个循环中处理两个迭代器,如下所示:
every
Iterator
object holds own memory location (adress), so you can't simply "merge" them. except if you extenditerator
class and write your own implementation there.If you are dealing with the same number of objects in both iterators an alternative solution would be to process two iterators in one loop like this :