Class PeekingIterator<E>
- Type Parameters:
- E- the type of elements returned by this iterator.
- All Implemented Interfaces:
- 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().
 
- Since:
- 4.0
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionelement()Returns the next element in iteration without advancing the underlying iterator.booleanhasNext()next()Returns the next element in iteration.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.voidremove()Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.IteratorforEachRemaining
- 
Constructor Details- 
PeekingIteratorConstructs a new instance.- Parameters:
- iterator- the iterator to decorate
 
 
- 
- 
Method Details- 
peekingIteratorDecorates 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:
- NullPointerException- if the iterator is null
 
- 
elementReturns the next element in iteration without advancing the underlying iterator. If the iterator is already exhausted, null will be returned.Note that if the underlying iterator is a FilterIteratoror aFilterListIterator, the underlying predicate will not be tested if element() orpeek()has been called after the most recent invocation ofnext()- Returns:
- the next element from the iterator
- Throws:
- NoSuchElementException- if the iterator is already exhausted according to- hasNext()
 
- 
hasNext
- 
nextReturns the next element in iteration.Note that if the underlying iterator is a FilterIteratoror aFilterListIterator, the underlying predicate will not be tested ifelement()orpeek()has been called after the most recent invocation ofnext().- Specified by:
- nextin interface- Iterator<E>
- Returns:
- the next element from the iterator
- Throws:
- NoSuchElementException- if the iterator is already exhausted according to- hasNext().
 
- 
peekReturns 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.Note that if the underlying iterator is a FilterIteratoror aFilterListIterator, the underlying predicate will not be tested ifelement()or peek() has been called after the most recent invocation ofnext().- Returns:
- the next element from the iterator
 
- 
remove
 
-