Given two sorted iterators. Implement IntersectionIterator which returns only common elements in both iterators. If you are not familiar with Iterators check similar questions.
public class IntersectionIterator implements Iterator<Integer> {
public IntersectionIterator(Iterator<Integer> it1, Iterator<Integer> it2) {
}
/**
* Returns the next element in the iteration (common element in the two iterators).
*/
public boolean hasNext() {
}
/**
* Returns true if the iteration has more elements (common elements in the two interators).
*/
public Integer next() {
}
}Example 1:
IntersectionIterator it = new IntersectionIterator([1, 2, 4, 5, 6], [1, 3, 5]);
it.hasNext(); // true
it.next(); // 1
it.next(); // 5
it.hasNext(); // false
it.next(); // errorExample 2:
IntersectionIterator it = new IntersectionIterator([1, 2, 4, 5, 6], [3, 7, 8, 9]);
it.hasNext(); // falseExample 3:
IntersectionIterator it = new IntersectionIterator([1, 2, 3, 4], [1, 2, 3, 4]);
it.hasNext(); // true
it.next(); // 1
it.next(); // 2
it.next(); // 3
it.next(); // 4
it.hasNext(); // false