Web / Apache Commons Collections Interview questions
What are Transformers in Apache Commons Collections?
A Transformer<I,O> defines one method, transform(I input), that converts an input object into an output object of possibly a different type.
Transformer<String, Integer> toLength = String::length; List<String> words = Arrays.asList("cat", "elephant", "dog"); Collection<Integer> lengths = CollectionUtils.collect(words, toLength); // [3, 8, 3]
TransformerUtils provides prebuilt variants such as constantTransformer() (always returns a fixed value), chainedTransformer() (applies several transformers in sequence), and invokerTransformer() (invokes a named method reflectively on the input).
Transformers pair with CollectionUtils.collect() for element-by-element mapping and with TransformedMap/TransformedCollection to normalize values automatically on insertion.
More Related questions...