Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
42 lines (36 loc) · 1.15 KB

File metadata and controls

42 lines (36 loc) · 1.15 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
class PeekingIterator implements Iterator<Integer> {
private Iterator<Integer> iterator;
private Integer nextElement = null;
private boolean hasPeeked = false;
public PeekingIterator(Iterator<Integer> iterator) {
// initialize any member here.
this.iterator = iterator;
}
// Returns the next element in the iteration without advancing the iterator.
public Integer peek() {
if (hasPeeked == true) {
return nextElement;
}
nextElement = iterator.next();
hasPeeked = true;
return nextElement;
}
// hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
@Override
public Integer next() {
if (hasPeeked == false) {
return iterator.next();
}
hasPeeked = false;
Integer result = nextElement;
nextElement = null;
return result;
}
@Override
public boolean hasNext() {
return hasPeeked || iterator.hasNext();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.