Web / Apache Commons Collections Interview questions
What is a TransformedCollection in Apache Commons Collections?
A TransformedCollection is a decorator that automatically runs a Transformer on every element as it's added, storing the transformed result instead of the original object.
Transformer<String, String> upper = String::toUpperCase; Collection<String> normalized = TransformedCollection.transformingCollection(new ArrayList<>(), upper); normalized.add("hello"); System.out.println(normalized); // [HELLO]
Using transformingCollection() only transforms elements added from that point forward, while the related decorate()-style factory that also processes existing elements up front lets you retroactively normalize a collection that already has content.
This is useful for enforcing consistent formatting - like always storing strings uppercased, trimmed, or converted to a canonical type - at a single choke point rather than repeating the transformation logic at every insertion site in the codebase.
More Related questions...