Class PeekingIterator<E>
java.lang.Object
org.apache.commons.collections4.iterators.PeekingIterator<E>
- Type Parameters:
E
- the type of elements returned by this iterator.
- All Implemented Interfaces:
Iterator<E>
Decorates an iterator to support one-element lookahead while iterating.
The decorator supports the removal operation, but an IllegalStateException
will be thrown if remove()
is called directly after a call to
peek()
or element()
.
- Since:
- 4.0
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionelement()
Returns the next element in iteration without advancing the underlying iterator.boolean
hasNext()
next()
peek()
Returns the next element in iteration without advancing the underlying iterator.static <E> PeekingIterator<E>
peekingIterator
(Iterator<? extends E> iterator) Decorates the specified iterator to support one-element lookahead.void
remove()
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
-
PeekingIterator
Constructs a new instance.- Parameters:
iterator
- the iterator to decorate
-
-
Method Details
-
peekingIterator
Decorates the specified iterator to support one-element lookahead.If the iterator is already a
PeekingIterator
it is returned directly.- Type Parameters:
E
- the element type- Parameters:
iterator
- the iterator to decorate- Returns:
- a new peeking iterator
- Throws:
NullPointerException
- if the iterator is null
-
element
Returns the next element in iteration without advancing the underlying iterator. If the iterator is already exhausted, null will be returned.- Returns:
- the next element from the iterator
- Throws:
NoSuchElementException
- if the iterator is already exhausted according tohasNext()
-
hasNext
-
next
-
peek
Returns the next element in iteration without advancing the underlying iterator. If the iterator is already exhausted, null will be returned.Note: this method does not throw a
NoSuchElementException
if the iterator is already exhausted. If you want such a behavior, useelement()
instead.The rationale behind this is to follow the
Queue
interface which uses the same terminology.- Returns:
- the next element from the iterator
-
remove
-