Web / Apache Commons Collections Interview questions
What is a Factory in Apache Commons Collections?
A Factory<T> defines one no-argument method, create(), that produces a new instance of type T on demand - useful for deferring object creation until it's actually needed.
Factory<ArrayList<String>> listFactory = ArrayList::new; Map<String, List<String>> lazyMap = MapUtils.lazyMap(new HashMap<>(), listFactory); lazyMap.get("groupA").add("item1"); // list is created automatically on first access
FactoryUtils provides constantFactory() (always returns the same instance), prototypeFactory() (clones a prototype object), and instantiateFactory() (calls a class's no-arg constructor reflectively).
Factories are most often paired with MapUtils.lazyMap() so a missing key gets a freshly created default value instead of null, similar in spirit to Map.computeIfAbsent() but usable without a Java 8 baseline.
More Related questions...