I have an ArrayList
to be filtered, and various Guava Predicate
s to filter it with. This list will have only 50-100 elements.
I was planning on
I agree with Peter's anwser.
But if you want to have fun, you could also wrap each of your predicate inside a predicate which would delegate to the wrapped predicate, and store the values for which true is returned inside a Map
:
class PredicateWrapper implements Predicate {
private Predicate delegate;
public PredicateWrapper(Predicate delegate) {
this.delegate = delegate;
}
@Override
public boolean apply(Foo foo) {
boolean result = delegate.apply(foo);
if (result) {
List objectsRemoved = removedByPredicate.get(delegate);
if (objectsRemoved == null) {
objectsRemoved = Lists.newArrayList();
removedByPredicate.put(delegate, objectsRemoved);
}
objectsRemoved.add(foo);
}
return result;
}
}