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.

- 18,217
- 15
- 74
- 89
-
What could be requirement for this.If you just want to loop forever some elements then why to use Iterator? – Khangharoth Jun 19 '09 at 08:34
-
If I were only using the iterator to access the objects via a while loop then there would be no point, but I'm not so I use an iterator as it is the best fit for what I want to do. – Bobby Jun 20 '09 at 00:27
9 Answers
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.)
-
The same functionality using [Apache Commons Collections](https://commons.apache.org/proper/commons-collections/): `Iterator
infinite = new LoopingIterator(names);` – Omid Jan 15 '22 at 15:40 -
or as `Iterables.cycle` Javadoc states `Stream.generate(() -> collection).flatMap(Collection::stream).iterator()` – Edgar Asatryan Jan 11 '23 at 15:16
Iterator it = mylist.iterator();
while (it.hasNext())
{
MyType t = (MyType)it.next();
// do something
if (!it.hasNext())
it = mylist.iterator();
}

- 1,395
- 14
- 26
-
-
-
1Ya, but at the bottom, you replace `it` with a new iterator if `it` is exausted, which means you will never come out of the while loop... which I guess is the whole point, but I misread the question and thought the OP was asking how to *create* an iterator that loops infinitely, which this doesn't do – Michael Hewson Sep 26 '16 at 02:05
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.

- 102,010
- 123
- 446
- 597
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.

- 3,529
- 4
- 26
- 29
-
I understand you posted your answer before mine, but as seen in my answer that is what I am basically doing right now, I was just wondering if there was a better way than instantiating a new Iterator object every time I get to the end – Bobby Jun 19 '09 at 06:39
This is what I can think of...
iterator = set.getIterator
//other code
if (iterator.hasNext())
//do code here
else
iterator = set.getIterator();

- 18,217
- 15
- 74
- 89
How about ?
List<String> list = // ArraysList
Interator<String> it = null;
while(true) {
it = list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}

- 1,353
- 3
- 19
- 27
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.

- 28,488
- 11
- 69
- 85
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.

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

- 386
- 1
- 10
-
Actually, it's a compile error because generate accepts `Supplier`. See my comment here: https://stackoverflow.com/questions/1016605/how-can-i-make-an-iterator-that-never-ends#comment132502479_1016650 – Edgar Asatryan Jan 11 '23 at 15:22