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