Skip to content

Instantly share code, notes, and snippets.

@cls
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cls/3cfaba9c6c185e976a48 to your computer and use it in GitHub Desktop.
Save cls/3cfaba9c6c185e976a48 to your computer and use it in GitHub Desktop.
Adaptor for covariant iterators in Java
import java.util.Iterator;
public class CovariantIterator<T extends U, U> implements Iterator<U>
{
private Iterator<T> iter;
public CovariantIterator(Iterator<T> it)
{
this.iter = it;
}
public CovariantIterator(Iterable<T> it)
{
this(it.iterator());
}
@Override
public boolean hasNext()
{
return this.iter.hasNext();
}
@Override
public U next()
{
return (U) this.iter.next();
}
@Override
public void remove()
{
this.iter.remove();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment