You could use apache commons collection implementing a Predicate for it.
http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html
Sample:
package snippet;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
public class TestCollection {
public static class User {
private String name;
public User(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [name=" + name + "]";
}
}
public static void main(String[] args) {
Collection<User> users = Arrays.asList(new User("User Name 1"), new User("User Name 2"), new User("Another User"));
Predicate predicate = new Predicate() {
public boolean evaluate(Object object) {
return ((User) object).getName().startsWith("User");
}
};
Collection filtered = CollectionUtils.select(users, predicate);
System.out.println(filtered);
}
}
A few sample can be found here:
http://apachecommonstipsandtricks.blogspot.de/2009/01/examples-of-functors-transformers.html
And if you need something more generic, like inspect a value of a specific field or property you could do something like:
public static class MyPredicate implements Predicate {
private Object expected;
private String propertyName;
public MyPredicate(String propertyName, Object expected) {
super();
this.propertyName = propertyName;
this.expected = expected;
}
public boolean evaluate(Object object) {
try {
return expected.equals(PropertyUtils.getProperty(object, propertyName));
} catch (Exception e) {
return false;
}
}
}
That could compare a specific property to a expected value, and the use would be something like:
Collection filtered = CollectionUtils.select(users, new MyPredicate("name", "User Name 2"));