public interface PeekableLongIterator extends LongIterator
Modifier and Type | Method and Description |
---|---|
void |
advanceIfNeeded(long minval)
If needed, advance as long as the next value is smaller than minval
The advanceIfNeeded method is used for performance reasons, to skip
over unnecessary repeated calls to next.
|
PeekableLongIterator |
clone()
Creates a copy of the iterator.
|
long |
peekNext()
Look at the next value without advancing
The peek is useful when working with several iterators at once.
|
hasNext, next
void advanceIfNeeded(long minval)
PeekableLongIterator j = // get an iterator
long val = // first value from my other data structure
j.advanceIfNeeded(val);
while ( j.hasNext() ) {
if(j.next() == val) {
// ah! ah! val is in the intersection...
// do something here
val = // get next value?
}
j.advanceIfNeeded(val);
}
The benefit of calling advanceIfNeeded is that each such call
can be much faster than repeated calls to "next". The underlying
implementation can "skip" over some data.minval
- thresholdlong peekNext()
PriorityQueue pq = new PriorityQueue(100,
new Comparator<PeekableIntIterator>() {
public int compare(PeekableIntIterator a,
PeekableIntIterator b) {
return a.peek() - b.peek();
}
});
//... populate pq
while(! pq.isEmpty() ) {
// get iterator with a smallest value
PeekableLongIterator pi = pq.poll();
long x = pi.next(); // advance
// do something with x
if(pi.hasNext()) pq.add(pi)
}
Notice how the peek method allows you to compare iterators in a way
that the next method could not do.PeekableLongIterator clone()
clone
in interface LongIterator
Copyright © 2018. All rights reserved.