#431 Filtering Iterator by Predicate

Companies:
Yandex
Themes:
JavaCollections & GenericsPractice

Implement a filtering iterator that finds the next element matching a predicate:

import java.util.Iterator;
import java.util.function.Predicate;

public class FilteringIterator<T> implements Iterator<T> {

    private final Predicate<T> predicate;
    private final Iterator<T> iterator;

    public FilteringIterator(Predicate<T> predicate, Iterator<T> iterator) {
        this.predicate = predicate;
        this.iterator = iterator;
    }

    @Override
    public boolean hasNext() {
        return false;
    }

    @Override
    public T next() {
        return null;
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in