'How can I make an iterator that never ends?

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.



Solution 1:[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.)

Solution 2:[2]

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

  // do something

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

Solution 3:[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.

Solution 4:[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.

Solution 5:[5]

This is what I can think of...

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

Solution 6:[6]

How about ?

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

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

Solution 7:[7]

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.

Solution 8:[8]

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.

Solution 9:[9]

std jdk:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kdb
Solution 2 AlexC
Solution 3
Solution 4 mnuzzo
Solution 5 Bobby
Solution 6 bluelurker
Solution 7 Emanuel Moecklin
Solution 8 Malid
Solution 9 bernstein