E
- the type of elements returned by this iterator.public class PeekingIterator<E> extends Object implements ListIterator<E>, LookaheadIterator<E>
The decorator supports the removal operation, but an IllegalStateException
will be thrown if remove()
, #add()
, #set()
} is called directly after a call to
peek()
or element()
.
Constructor and Description |
---|
PeekingIterator(List<E> list)
Constructor.
|
PeekingIterator(ListIterator<E> iterator)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
add(E e) |
int |
currentIndex() |
E |
element()
Returns the next element in iteration without advancing the underlying iterator.
|
boolean |
hasNext() |
boolean |
hasPrevious() |
E |
next() |
int |
nextIndex() |
E |
peek()
Returns the next element in iteration without advancing the underlying iterator.
|
<E> PeekingIterator<E> |
peekingIterator(ListIterator<E> iterator)
Decorates the specified iterator to support one-element lookahead.
|
E |
previous() |
int |
previousIndex() |
void |
remove() |
void |
set(E e) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEachRemaining
public PeekingIterator(ListIterator<E> iterator)
iterator
- the iterator to decoratepublic <E> PeekingIterator<E> peekingIterator(ListIterator<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 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.
peek
in interface LookaheadIterator<E>
public E element()
element
in interface LookaheadIterator<E>
NoSuchElementException
- if the iterator is already exhausted according to hasNext()
public E next()
public void remove()
remove
in interface Iterator<E>
remove
in interface ListIterator<E>
IllegalStateException
- if peek()
or element()
has been called
prior to the call to remove()
public boolean hasPrevious()
hasPrevious
in interface ListIterator<E>
public E previous()
previous
in interface ListIterator<E>
public int nextIndex()
nextIndex
in interface ListIterator<E>
public int currentIndex()
public int previousIndex()
previousIndex
in interface ListIterator<E>
public void set(E e)
set
in interface ListIterator<E>
public void add(E e)
add
in interface ListIterator<E>
Copyright © 2007–2023. All rights reserved.