如何制作一个永不结束的迭代器?

发布于 2024-07-25 03:56:41 字数 117 浏览 10 评论 0原文

我只是想知道无限期地迭代集合的最简单方法是什么,即当它到达末尾时,它 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

情未る 2024-08-01 03:56:41

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

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

我不推荐 Google Collections 库因为我在 Google 工作,所以它非常有偏见,但我认为几乎每个编写 Java 的 Google 员工都会告诉您这些集合有多么有用。)

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

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

(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.)

殤城〤 2024-08-01 03:56:41
Iterator it = mylist.iterator();
while (it.hasNext())
{
  MyType t = (MyType)it.next();

  // do something

  if (!it.hasNext())
    it = mylist.iterator();
}
Iterator it = mylist.iterator();
while (it.hasNext())
{
  MyType t = (MyType)it.next();

  // do something

  if (!it.hasNext())
    it = mylist.iterator();
}
旧时光的容颜 2024-08-01 03:56:41

尝试 EndlessIterator< /a> 来自 Cactoos

Iterator<String> names = new EndlessIterator<>("John");

它将始终返回 "John" 并且永远不会结束。

另外,检查 EndlessIterable,它实现了 Iterable 并执行相同的操作。

Try EndlessIterator from Cactoos:

Iterator<String> names = new EndlessIterator<>("John");

It will always return "John" and will never end.

Also, check EndlessIterable, which implements Iterable and does the same.

国产ˉ祖宗 2024-08-01 03:56:41

如果您正在创建迭代器,则在下一个方法中您可以使用 if 条件来检查列表中是否有另一个对象。 如果有,则返回该对象,如果没有,则返回到列表的开头并返回该对象。

If you're making the iterator, in the next method you can have an if condition that checks if there's another object in the list. If there is, then you return that object, if there isn't then you go back to the start of the list and return that object.

趴在窗边数星星i 2024-08-01 03:56:41

这是我能想到的...

iterator = set.getIterator
//other code
if (iterator.hasNext())
    //do code here
else
    iterator = set.getIterator();

This is what I can think of...

iterator = set.getIterator
//other code
if (iterator.hasNext())
    //do code here
else
    iterator = set.getIterator();
腹黑女流氓 2024-08-01 03:56:41

怎么样 ?

List<String> list = // ArraysList
Interator<String> it = null;

while(true) {
 it = list.iterator();
 while(it.hasNext()) {
   System.out.println(it.next());
 }
}

How about ?

List<String> list = // ArraysList
Interator<String> it = null;

while(true) {
 it = list.iterator();
 while(it.hasNext()) {
   System.out.println(it.next());
 }
}
自我难过 2024-08-01 03:56:41

如果您不想使用 Guava 但仍想要一个可重用的解决方案:

public static class CyclicIterator<E, C extends Collection<E>> implements Iterator<E> {
    final private C mElements;
    private Iterator<E> mIterator;

    public CyclicIterator(C elements) {
        mElements = elements;
        mIterator = elements.iterator();
    }

    @Override
    public boolean hasNext() {
        if (! mIterator.hasNext()) {
            mIterator = mElements.iterator();
        }
        return mIterator.hasNext();
    }

    @Override
    public E next() {
        if (! mIterator.hasNext()) {
            mIterator = mElements.iterator();
        }
        return mIterator.next();
    }
}

注意:这不支持 remove() 方法,但如果需要,可以轻松添加。 而且它也不是线程安全的。

If you don't want to use Guava but still want a reusable solution:

public static class CyclicIterator<E, C extends Collection<E>> implements Iterator<E> {
    final private C mElements;
    private Iterator<E> mIterator;

    public CyclicIterator(C elements) {
        mElements = elements;
        mIterator = elements.iterator();
    }

    @Override
    public boolean hasNext() {
        if (! mIterator.hasNext()) {
            mIterator = mElements.iterator();
        }
        return mIterator.hasNext();
    }

    @Override
    public E next() {
        if (! mIterator.hasNext()) {
            mIterator = mElements.iterator();
        }
        return mIterator.next();
    }
}

Note: this doesn't support the remove() method but it could easily be added if needed. Also it's not thread safe.

酒解孤独 2024-08-01 03:56:41

我认为你想要的永远不会有帮助你可以用你的迭代器做任何事情,这很简单,但是你必须小心你添加的任何新东西,我不使用这种风格,但这就是你想要的:

if (! It.hasNext() )
{
while ( It.hasPrevious() )
{
它 = It.Previous();
}
} 别的 {
它 = It.Next();
如果

您真的感兴趣,这种方法没什么用,您应该在推送新列表时始终将最后一个指针指向第一个。

I think what you want never help You can do anything with your iterator that's easy but you must be carefull with any new thing you add im not used with this style but this is what you want though :

if (! It.hasNext() )
{
while ( It.hasPrevious() )
{
It = It.Previous();
}
} else {
It = It.Next();
}

This way is nothing if your really interested you should instead make next pointer of the last to the first always when pushing a new list.

千里故人稀 2024-08-01 03:56:41

标准jdk:

Iterable<String> infinite = Stream.generate(names.stream()).flatMap(e -> e).iterator()

std jdk:

Iterable<String> infinite = Stream.generate(names.stream()).flatMap(e -> e).iterator()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文