Advertisement
Guest User

javahelp

a guest
Oct 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. package iterators;
  2.  
  3.  
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.LinkedList;
  7.  
  8. // Iterator that uses a Predicate to filter out elements from the input
  9. public class Filter<T> extends FlatApply<T,T> {
  10.     public Filter(Predicate<T> p, Iterator<T> input) {
  11.         // you DO NOT need to modify the constructor
  12.         super(new FilteringFlatApplyFunction<>(p), input);
  13.     }  
  14.  
  15.     // uses a Predicate to decide whether the input element is output or not
  16.     private static class FilteringFlatApplyFunction<T> implements FlatApplyFunction<T, T> {
  17.            
  18.             //You will need to create a new List because you are given a T as input.
  19. //            private Iterator<T> input;
  20.             private List newList = new LinkedList();
  21.             private T temp;
  22.            
  23.             public List<T> apply(Predicate<T> p, Iterator<T> input){
  24.                 temp = input.next();
  25.                 if(p.check(temp)){
  26.                     newList.add(temp);
  27.                 }
  28.                 return newList;
  29.     }
  30.  
  31.     // You DO NOT need to define hasNext() and next(). FlatApply provides them already.
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement