如何制作永不结束的迭代器? [英] How can I make an iterator that never ends?

查看:119
本文介绍了如何制作永不结束的迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道无限期地迭代一个集合的最简单方法是什么,即当它到达结尾时 next(); 调用第一个对象。我假设这不是Java中已经预定义的函数,所以只是寻找用Java实现它的最简单方法。

I was just wondering what the easiest way to iterate over a set indefinitely, i.e. when it reaches the end it next(); calls the first object. I'm assuming that this is not an already predefined function in Java, so just looking for the easiest way to implement this in Java.

推荐答案

优秀的 Google Collections 库中有一种方法可以执行此操作:

There's a method in the excellent Google Collections library which does this:

Set<String> names = ...;
Iterable<String> infinite = Iterables.cycle(names);

(我不能足够强烈地推荐Google Collections库。它非常努力。我是我在谷歌工作时有偏见,但我认为几乎所有编写Java的Googler都会告诉你这些收藏品有多么有用。)

(I can't recommend the Google Collections library strongly enough. It rocks very hard. I'm biased as I work for Google, but I think pretty much every Googler writing Java would tell you how useful the collections are.)

这篇关于如何制作永不结束的迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆