public class PeekingIterator<E> extends Object implements 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<? extends E> 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<E> |
peekingIterator(Iterator<? extends E> iterator)
Decorates the specified iterator to support one-element lookahead.
|
void |
remove() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEachRemaining
public PeekingIterator(Iterator<? extends E> iterator)
iterator
- the iterator to decoratepublic static <E> PeekingIterator<E> peekingIterator(Iterator<? extends E> iterator)
If the iterator is already a PeekingIterator
it is returned directly.
E
- the element typeiterator
- the iterator to decorateNullPointerException
- if the iterator is nullpublic 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()
Copyright © 2001–2019 The Apache Software Foundation. All rights reserved.