Advertisement
Guest User

javahelp2

a guest
Oct 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. Here is a motivating query. Let's suppose we have a list of numbers [10,50,1,400]. This list will
  2. be our input data. The query is "return the elements greater than 40". The output for this
  3. example would be the elements 50, 400.
  4.  
  5. As you saw in TextQuery2.java, we can use FlatApply to remove elements that don't pass a
  6. check (e.g., only return words longer than 12 characters). This removal of data based on a
  7. checking some property is commonly known as "filtering". Filtering is so common that it is
  8. worth implementing another Iterator called Filter.
  9.  
  10. Because Filter just a special case of FlatApply, we will use FlatApply to build Filter. In particular,
  11. we will use Java's concept of inheritance to borrow most of the functionality from the FlatApply
  12. class.
  13.  
  14. Take a look at Filter.java. You'll see that it "extends" the FlatApply class.
  15. public class Filter<T> extends FlatApply<T,T> {
  16. "Extends" is a lot like "implements", except that you use it on a class instead of an interface.
  17. The "extends" here means that that a Filter<T> is a FlatApply<T,T>. Therefore, a Filter<T>
  18. already has the useful next() and hasNext() methods defined by FlatApply.
  19.  
  20. NOTE: Why extend FlatApply<T,T>? Unlike FlatApply, Filter never changes the type of the data;
  21. it only keeps or removes each element. Therefore, the input type T is the same as the output
  22. type T.
  23.  
  24. What you need to do
  25.  
  26. Implement the Filter class. You only need to fill in the inner class FilteringFlatApplyFunction.
  27. You do not need to override the next() and hasNext() methods, and you do not need to edit
  28. the constructor.
  29.  
  30. The first argument to the Filter constructor is a Predicate<T>. The Predicate interface's one
  31. method, Predicate.check, is like ApplyFunction.apply, except it returns a boolean value. True
  32. indicates return the element; false indicates ignore the element.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement