public class PeekingIterator<E> extends Objectimplements Iterator <E>
The decorator supports the removal operation, but an IllegalStateException will be thrown if remove() is called directly after a call to peek() or element().
| Constructor and Description |
|---|
PeekingIterator(Iterator
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
E |
element()
Returns the next element in iteration without advancing the underlying iterator.
|
boolean |
hasNext()
|
E |
next()
|
E |
peek()
Returns the next element in iteration without advancing the underlying iterator.
|
static <E> PeekingIterator |
peekingIterator(Iterator
Decorates the specified iterator to support one-element lookahead.
|
void |
remove()
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitforEachRemainingpublic static <E> PeekingIterator<E> peekingIterator(Iterator <? extends E> iterator)
If the iterator is already a PeekingIterator it is returned directly.
E - the element type
iterator - the iterator to decorate
NullPointerException - if the iterator is null
public boolean hasNext()
public E peek()
Note: this method does not throw a NoSuchElementException if the iterator is already exhausted. If you want such a behavior, use element() instead.
The rationale behind this is to follow the Queue interface which uses the same terminology.
public E element()
NoSuchElementException - if the iterator is already exhausted according to
hasNext()
public E next()