How can I make an iterator that never ends?

百般思念 提交于 2019-12-08 17:06:37

问题


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.


回答1:


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




回答2:


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

  // do something

  if (!it.hasNext())
    it = mylist.iterator();
}



回答3:


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.




回答4:


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.




回答5:


This is what I can think of...

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



回答6:


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.




回答7:


How about ?

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

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



回答8:


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.



来源:https://stackoverflow.com/questions/1016605/how-can-i-make-an-iterator-that-never-ends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!