Web / Apache Commons Collections Interview questions
How is FactoryUtils used to lazily create objects?
FactoryUtils wraps different object-creation strategies behind the single-method Factory<T> interface, so creation logic can be deferred and swapped without touching the code that eventually calls create().
Factory<List<String>> listFactory = FactoryUtils.instantiateFactory(ArrayList.class); Map<String, List<String>> groups = MapUtils.lazyMap(new HashMap<>(), listFactory); groups.get("teamA").add("Alice"); // list is created on first access, not before
instantiateFactory(Class) calls a class's no-arg constructor reflectively only when create() actually runs; constantFactory(value) always hands back the same pre-built instance; and prototypeFactory(prototype) returns a clone of a prototype object each time, useful when you want independent copies rather than one shared reference.
Paired with MapUtils.lazyMap(), a Factory means a missing key gets a freshly created default value transparently on first get(), instead of the caller getting back null and having to check-then-create manually - the same idea Java 8 later formalized as Map.computeIfAbsent(), but pluggable through any Factory strategy and usable in codebases that predate Java 8.
More Related questions...