Is it possible to merge iterators in Java? I have two iterators and I want to combine/merge them so that I could iterate though their elements in one go (in same loop) rathe
public class IteratorJoin implements Iterator {
private final Iterator first, next;
public IteratorJoin(Iterator first, Iterator next) {
this.first = first;
this.next = next;
}
@Override
public boolean hasNext() {
return first.hasNext() || next.hasNext();
}
@Override
public T next() {
if (first.hasNext())
return first.next();
return next.next();
}
}