Web / Apache Commons Collections Interview questions
What are Predicates in Apache Commons Collections?
A Predicate<T> is a functional interface with a single method, evaluate(T object), that returns true or false - essentially a reusable, named condition.
Predicate<String> isLong = s -> s.length() > 5; List<String> names = Arrays.asList("Al", "Alexandra", "Bo", "Bartholomew"); Collection<String> longNames = CollectionUtils.select(names, isLong);
PredicateUtils supplies ready-made combinators - andPredicate(), orPredicate(), notPredicate(), allPredicate(), anyPredicate() - so multiple conditions can be composed into a single Predicate object instead of writing nested if statements.
Predicates plug directly into CollectionUtils.filter(), select(), and selectRejected() for filtering collections declaratively.
More Related questions...