Web / Apache Commons Collections Interview questions
Why should you use TransformedMap instead of manual validation in setters?
Manual validation scattered across setters relies on every developer remembering to call the check before every insertion - a pattern that tends to erode as a codebase grows, since a new insertion path added later can easily forget to include the same validation the original ones had.
Transformer<String, String> normalizeKey = String::toLowerCase; Map<String, Integer> inventory = TransformedMap.transformingMap(new HashMap<>(), normalizeKey, null); inventory.put("Widget", 10); inventory.get("widget"); // 10 - key was lowercased automatically on insert
TransformedMap moves that logic into the map itself: any key and/or value transformer supplied at construction runs automatically on every put(), putAll(), and even entries added through the map's entrySet() iterator, so there's exactly one place the normalization rule lives instead of one per call-site.
This matters most in larger teams or long-lived codebases, where the risk isn't writing the validation once correctly - it's someone else adding a new insertion path six months later and not knowing the rule existed at all; centralizing it in the map removes that failure mode entirely.
More Related questions...