A
StringFilter object can be passed to a method that uses it to filter
longer strings from a list of strings, as in the following test class:
public class FilterTester
{
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");
list.add("six");
list.add("seven");
ArrayList<String> newList = filter(list, new StringFilter());
System.out.println(newList); // should display [one, two, six]
}
public static ArrayList<String> filter(ArrayList<String> a, Filter f)
{
ArrayList<String> newList = new ArrayList<String>();
Iterator<String> iter = a.iterator();
while ( iter.hasNext() ) {
String item = iter.next();
if ( f.accept(item) )
newList.add(item);
}
return newList;
}
}