I receive an iterator as argument and I would like to iterate on values twice.
public void reduce(Pair key, Iterator
If method signature cannot be changed then I would suggest using Apache Commons IteratorUtils to convert Iterator to ListIterator. Consider this example method for iterating twice on values:
void iterateTwice(Iterator it) {
ListIterator> lit = IteratorUtils.toListIterator(it);
System.out.println("Using ListIterator 1st pass");
while(lit.hasNext())
System.out.println(lit.next());
// move the list iterator back to start
while(lit.hasPrevious())
lit.previous();
System.out.println("Using ListIterator 2nd pass");
while(lit.hasNext())
System.out.println(lit.next());
}
Using code like above I was able to iterate over the list of values without saving a copy of List elements in my code.