Class LoopingIterator<E>
- Type Parameters:
E
- the type of elements returned by this iterator.
- All Implemented Interfaces:
Iterator<E>
,ResettableIterator<E>
The iterator will loop continuously around the provided elements, unless
there are no elements in the collection to begin with, or all the elements
have been removed
.
Concurrent modifications are not directly supported, and for most collection implementations will throw a ConcurrentModificationException.
- Since:
- 3.0
-
Constructor Summary
ConstructorDescriptionLoopingIterator
(Collection<? extends E> collection) Constructor that wraps a collection. -
Method Summary
Modifier and TypeMethodDescriptionboolean
hasNext()
Has the iterator any more elements.next()
Returns the next object in the collection.void
remove()
Removes the previously retrieved item from the underlying collection.void
reset()
Resets the iterator back to the start of the collection.int
size()
Gets the size of the collection underlying the iterator.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
LoopingIterator
Constructor that wraps a collection.There is no way to reset an Iterator instance without recreating it from the original source, so the Collection must be passed in.
- Parameters:
collection
- the collection to wrap- Throws:
NullPointerException
- if the collection is null
-
-
Method Details
-
hasNext
Has the iterator any more elements.Returns false only if the collection originally had zero elements, or all the elements have been
removed
. -
next
Returns the next object in the collection.If at the end of the collection, return the first element.
- Specified by:
next
in interfaceIterator<E>
- Returns:
- the next object
- Throws:
NoSuchElementException
- if there are no elements at all. UsehasNext()
to avoid this error.
-
remove
Removes the previously retrieved item from the underlying collection.This feature is only supported if the underlying collection's
iterator
method returns an implementation that supports it.This method can only be called after at least one
next()
method call. After a removal, the remove method may not be called again until another next has been performed. If thereset()
is called, then remove may not be called untilnext()
is called again. -
reset
Resets the iterator back to the start of the collection.- Specified by:
reset
in interfaceResettableIterator<E>
-
size
Gets the size of the collection underlying the iterator.- Returns:
- the current collection size
-