public class PeekingIterator<E>
extends java.lang.Object
implements java.util.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(java.util.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(java.util.Iterator<? extends E> iterator)
Decorates the specified iterator to support one-element lookahead.
|
void |
remove() |
public PeekingIterator(java.util.Iterator<? extends E> iterator)
iterator
- the iterator to decoratepublic static <E> PeekingIterator<E> peekingIterator(java.util.Iterator<? extends E> iterator)
If the iterator is already a PeekingIterator
it is returned directly.
E
- the element typeiterator
- the iterator to decoratejava.lang.NullPointerException
- if the iterator is nullpublic boolean hasNext()
hasNext
in interface java.util.Iterator<E>
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()
java.util.NoSuchElementException
- if the iterator is already exhausted according to hasNext()
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"