I want to filter a java.util.Collection
based on a predicate.
The setup:
public interface Predicate {
public boolean filter(T t);
}
void filterCollection(Collection col, Predicate predicate) {
for (Iterator i = col.iterator(); i.hasNext();) {
T obj = i.next();
if (predicate.filter(obj)) {
i.remove();
}
}
}
The usage:
List myList = ...;
filterCollection(myList, new Predicate() {
public boolean filter(MyObject obj) {
return obj.shouldFilter();
}
});