problems: add peeking iterator

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-08-05 12:11:01 +02:00
parent 9dc591147a
commit a71684d500
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,32 @@
class PeekingIterator implements Iterator<Integer> {
private Iterator<Integer> it;
private Integer pVal;
private boolean pHasNext;
public PeekingIterator(Iterator<Integer> iterator) {
it = iterator;
next();
}
public Integer peek() {
return pVal;
}
@Override
public Integer next() {
pHasNext = it.hasNext();
Integer oldValue = pVal;
if (pHasNext) {
pVal = it.next();
}
return oldValue;
}
@Override
public boolean hasNext() {
return pHasNext;
}
}