Given an Iterator class interface with methods: next()
and hasNext()
, design and implement a PeekingIterator that support the peek()
operation -- it essentially peek() at the element that will be returned by the next call to next().
Here is an example. Assume that the iterator is initialized to the beginning of the list: [1, 2, 3]
.
Call next()
gets you 1, the first element in the list.
Now you call peek()
and it returns 2, the next element. Calling next()
after that still return 2.
You call next()
the final time and it returns 3, the last element. Calling hasNext()
after that should return false.
// Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
class PeekingIterator implements Iterator<Integer> {
<span style="white-space:pre"> </span>public PeekingIterator(Iterator<Integer> iterator) {
<span style="white-space:pre"> </span> // initialize any member here.
<span style="white-space:pre"> </span> iteratorPassedIn = iterator;
<span style="white-space:pre"> </span>}
// Returns the next element in the iteration without advancing the iterator.
<span style="white-space:pre"> </span>public Integer peek() {
<span style="white-space:pre"> </span> if(cachedResult == null){
<span style="white-space:pre"> </span> Integer returnValue = iteratorPassedIn.next();
cachedResult = returnValue;
return returnValue;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> else{
<span style="white-space:pre"> </span> return cachedResult;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>// hasNext() and next() should behave the same as in the Iterator interface.
<span style="white-space:pre"> </span>// Override them if needed.
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public Integer next() {
<span style="white-space:pre"> </span> if(cachedResult != null){
<span style="white-space:pre"> </span> Integer returnResult = new Integer(cachedResult);
<span style="white-space:pre"> </span> cachedResult = null;
<span style="white-space:pre"> </span> return returnResult;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> else{
<span style="white-space:pre"> </span> return iteratorPassedIn.next();
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public boolean hasNext() {
<span style="white-space:pre"> </span> if(cachedResult != null){
<span style="white-space:pre"> </span> return true;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> else{
<span style="white-space:pre"> </span> return iteratorPassedIn.hasNext();
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>private Integer cachedResult = null;
<span style="white-space:pre"> </span>private Iterator<Integer> iteratorPassedIn;
}