Top Description Fields Constructors Methods
org.apache.commons.collections4.iterators

public Class PeekingIterator<E>

extends Object
implements Iterator<E>
Class Inheritance
All Implemented Interfaces
java.util.Iterator
Imports
java.util.Iterator, .NoSuchElementException

Decorates an iterator to support one-element lookahead while iterating.

The decorator supports the removal operation, but an IllegalStateException will be thrown if remove() is called directly after a call to peek() or element().

Since
4.0

Field Summary

Modifier and TypeField and Description
private boolean
exhausted

Indicates that the decorated iterator is exhausted.

private final Iterator<? extends E>
iterator

The iterator being decorated.

private E
slot

The current slot for lookahead.

private boolean
slotFilled

Indicates if the lookahead slot is filled.

Constructor Summary

AccessConstructor and Description
public
PeekingIterator(final Iterator<? extends E>
the iterator to decorate
iterator
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public E

Returns:

the next element from the iterator
element
()

Returns the next element in iteration without advancing the underlying iterator.

private void
fill()

public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
public E
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.
public E

Returns:

the next element from the iterator
peek
()

Returns the next element in iteration without advancing the underlying iterator.

public static <
the element type
E
>
PeekingIterator<E>

Returns:

a new peeking iterator
peekingIterator
(final Iterator<? extends E>
the iterator to decorate
iterator
)

Decorates the specified iterator to support one-element lookahead.

public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

exhaustedback to summary
private boolean exhausted

Indicates that the decorated iterator is exhausted.

iteratorback to summary
private final Iterator<? extends E> iterator

The iterator being decorated.

slotback to summary
private E slot

The current slot for lookahead.

slotFilledback to summary
private boolean slotFilled

Indicates if the lookahead slot is filled.

Constructor Detail

PeekingIteratorback to summary
public PeekingIterator(final Iterator<? extends E> iterator)

Constructor.

Parameters
iterator:Iterator<? extends E>

the iterator to decorate

Method Detail

elementback to summary
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:E

the next element from the iterator

Exceptions
NoSuchElementException:
if the iterator is already exhausted according to hasNext()
fillback to summary
private void fill()
hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

Annotations
@Override
nextback to summary
public E next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:E

the next element in the iteration

Annotations
@Override
peekback to summary
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 NoSuchElementException if the iterator is already exhausted. If you want such a behavior, use element() instead.

The rationale behind this is to follow the java.util.Queue interface which uses the same terminology.

Returns:E

the next element from the iterator

peekingIteratorback to summary
public static <E> PeekingIterator<E> peekingIterator(final Iterator<? extends E> iterator)

Decorates the specified iterator to support one-element lookahead.

If the iterator is already a PeekingIterator it is returned directly.

Parameters
<E>
the element type
iterator:Iterator<? extends E>

the iterator to decorate

Returns:PeekingIterator<E>

a new peeking iterator

Exceptions
NullPointerException:
if the iterator is null
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

Annotations
@Override
Exceptions
IllegalStateException:
if peek() or element() has been called prior to the call to remove()