Class PeekingIterator<E>
- java.lang.Object
-
- org.apache.commons.collections4.iterators.PeekingIterator<E>
-
- All Implemented Interfaces:
java.util.Iterator<E>
public class PeekingIterator<E> extends java.lang.Object implements java.util.Iterator<E>Decorates an iterator to support one-element lookahead while iterating.The decorator supports the removal operation, but an
IllegalStateExceptionwill be thrown ifremove()is called directly after a call topeek()orelement().- Since:
- 4.0
-
-
Field Summary
Fields Modifier and Type Field Description private booleanexhaustedIndicates that the decorated iterator is exhausted.private java.util.Iterator<? extends E>iteratorThe iterator being decorated.private EslotThe current slot for lookahead.private booleanslotFilledIndicates if the lookahead slot is filled.
-
Constructor Summary
Constructors Constructor Description PeekingIterator(java.util.Iterator<? extends E> iterator)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Eelement()Returns the next element in iteration without advancing the underlying iterator.private voidfill()booleanhasNext()Enext()Epeek()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.voidremove()
-
-
-
Field Detail
-
iterator
private final java.util.Iterator<? extends E> iterator
The iterator being decorated.
-
exhausted
private boolean exhausted
Indicates that the decorated iterator is exhausted.
-
slotFilled
private boolean slotFilled
Indicates if the lookahead slot is filled.
-
slot
private E slot
The current slot for lookahead.
-
-
Constructor Detail
-
PeekingIterator
public PeekingIterator(java.util.Iterator<? extends E> iterator)
Constructor.- Parameters:
iterator- the iterator to decorate
-
-
Method Detail
-
peekingIterator
public static <E> PeekingIterator<E> peekingIterator(java.util.Iterator<? extends E> iterator)
Decorates the specified iterator to support one-element lookahead.If the iterator is already a
PeekingIteratorit is returned directly.- Type Parameters:
E- the element type- Parameters:
iterator- the iterator to decorate- Returns:
- a new peeking iterator
- Throws:
java.lang.NullPointerException- if the iterator is null
-
fill
private void fill()
-
hasNext
public boolean hasNext()
- Specified by:
hasNextin interfacejava.util.Iterator<E>
-
peek
public E 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
NoSuchElementExceptionif the iterator is already exhausted. If you want such a behavior, useelement()instead.The rationale behind this is to follow the
Queueinterface which uses the same terminology.- Returns:
- the next element from the iterator
-
element
public E 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:
java.util.NoSuchElementException- if the iterator is already exhausted according tohasNext()
-
-