Contents    Page-10    Prev    Next    Page+10    Index   

Filter Pattern

A filter is an important concept in CS. Just as a coffee filter removes coffee grounds while letting liquid pass through, a filter program removes items from a Collection if they meet some condition:


static void filter(Collection<?> c) {
  for (Iterator<?> it = c.iterator();
                   it.hasNext(); )
    if ( condition(it.next()) )
       it.remove();  }

This filter is destructive, removing items from the collection if they satisfy the condition. One can also write a constructive filter that makes a new collection, without modifying the original collection.