Skip to content

Instantly share code, notes, and snippets.

@jandk
Last active August 29, 2015 14:03
Show Gist options
  • Save jandk/a207b2f82d6c7986c054 to your computer and use it in GitHub Desktop.
Save jandk/a207b2f82d6c7986c054 to your computer and use it in GitHub Desktop.
import java.util.Iterator;
import java.util.NoSuchElementException;
public class IteratorWithCurrent<E> implements Iterator<E>
{
private final Iterator<E> it;
private E current;
public IteratorWithCurrent(Iterable<E> iterable) {
it = iterable.iterator();
}
public E current() {
if (current == null) {
throw new NoSuchElementException();
}
return current;
}
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public E next() {
current = it.next();
return current;
}
@Override
public void remove() {
it.remove();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment