Web / Apache Commons Collections Interview questions
What are Closures in Apache Commons Collections?
A Closure<T> defines one method, execute(T input), that performs a side effect on the input and returns nothing - the counterpart to Predicate (returns boolean) and Transformer (returns a new object).
Closure<String> printer = System.out::println; List<String> names = Arrays.asList("Ann", "Ben"); CollectionUtils.forAllDo(names, printer);
ClosureUtils supplies combinators like chainedClosure() (run several closures in order on the same input) and ifClosure() (run one closure or another depending on a Predicate's result).
Closures are typically used with CollectionUtils.forAllDo() when the goal is to act on every element - logging, updating state, sending notifications - rather than to filter or transform the collection itself.
More Related questions...