Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package iterators;
- import java.util.Iterator;
- import java.util.List;
- import java.util.LinkedList;
- // Iterator that uses a Predicate to filter out elements from the input
- public class Filter<T> extends FlatApply<T,T> {
- public Filter(Predicate<T> p, Iterator<T> input) {
- // you DO NOT need to modify the constructor
- super(new FilteringFlatApplyFunction<>(p), input);
- }
- // uses a Predicate to decide whether the input element is output or not
- private static class FilteringFlatApplyFunction<T> implements FlatApplyFunction<T, T> {
- //You will need to create a new List because you are given a T as input.
- // private Iterator<T> input;
- private List newList = new LinkedList();
- private T temp;
- public List<T> apply(Predicate<T> p, Iterator<T> input){
- temp = input.next();
- if(p.check(temp)){
- newList.add(temp);
- }
- return newList;
- }
- // You DO NOT need to define hasNext() and next(). FlatApply provides them already.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement