Web / Apache Commons Collections Interview questions
1. What is Apache Commons Collections?
Apache Commons Collections is an open-source Java library that extends the standard Java Collections Framework (JCF) with extra data structures and utility classes that the JDK doesn't ship with out of the box. It adds interfaces like Bag , BidiMap , and MultiValuedMap , along with functional-sty...
2. What are the main packages in Apache Commons Collections 4?
Commons Collections 4 organizes its classes under org.apache.commons.collections4 and a set of focused sub-packages rather than one large flat package. bag - Bag implementations like HashBag and TreeBag bidimap - DualHashBidiMap, DualTreeBidiMap, TreeBidiMap map - LRUMap, ReferenceMap, MultiKeyMa...
3. What is a Bag in Apache Commons Collections?
A Bag
4. What are the types of Bag implementations in Commons Collections?
Commons Collections ships two core, non-decorator Bag implementations, plus several decorators that layer behavior on top of either one. HashBag TreeBag backed by a HashMap; no ordering guarantee backed by a TreeMap; iterates in sorted order elements need only equals()/hashCode() elements must be...
5. What is a BidiMap in Apache Commons Collections?
A BidiMap
6. What is a MultiValuedMap in Apache Commons Collections?
A MultiValuedMap
7. What is the purpose of CollectionUtils in Apache Commons Collections?
CollectionUtils is a static helper class packed with null-safe operations for working with any java.util.Collection , so callers don't have to hand-roll the same boilerplate checks repeatedly. isEmpty(coll) / isNotEmpty(coll) - null-safe emptiness checks union() , intersection() , subtract() , di...
8. What is the purpose of MapUtils in Apache Commons Collections?
MapUtils provides static helpers for working with Map instances, focused on null-safety and type-safe value extraction. Methods like getString(map, key, defaultValue) , getInteger() , and getBoolean() pull a value out, cast it to the expected type, and fall back to a supplied default if the key i...
9. What is the purpose of ListUtils in Apache Commons Collections?
ListUtils focuses specifically on List-oriented operations that the JDK's Collections class doesn't cover directly. union() , intersection() , subtract() - combine two Lists while preserving duplicate counts, unlike Set-based operations isEqualList() - compares two Lists element-by-element, toler...
10. What are Predicates in Apache Commons Collections?
A Predicate
11. What are Transformers in Apache Commons Collections?
A Transformer defines one method, transform(I input) , that converts an input object into an output object of possibly a different type. Transformer
12. What are Closures in Apache Commons Collections?
A Closure
13. What is a Factory in Apache Commons Collections?
A Factory
14. Define CircularFifoQueue in Apache Commons Collections?
CircularFifoQueue
15. What is an LRUMap in Apache Commons Collections?
LRUMap
16. What is a ReferenceMap in Apache Commons Collections?
ReferenceMap
17. What is a MultiKeyMap in Apache Commons Collections?
MultiKeyMap
18. What is the purpose of IteratorUtils in Apache Commons Collections?
IteratorUtils is a static helper class for constructing and combining Iterator instances beyond what the JDK provides directly. chainedIterator() - walks through several iterators back to back as one filteredIterator() - skips elements that fail a Predicate transformedIterator() - applies a Trans...
19. Describe the LoopingIterator class in Apache Commons Collections?
LoopingIterator
20. What is an OrderedMap in Apache Commons Collections?
An OrderedMap
21. What is a SortedBidiMap in Apache Commons Collections?
A SortedBidiMap
22. What is the purpose of ComparatorUtils in Apache Commons Collections?
ComparatorUtils supplies static helpers for building and combining Comparator instances without writing a new class for each variation. Comparator
23. Define FixedOrderComparator in Apache Commons Collections?
FixedOrderComparator
24. What is a PredicatedCollection in Apache Commons Collections?
A PredicatedCollection is a decorator that wraps an existing Collection and validates every element against a supplied Predicate before allowing it to be added. Predicate
25. 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.transfor...
26. What is the difference between a Map and a MultiValuedMap?
A standard Map
27. What is the difference between a BidiMap and a regular Map?
A regular Map
28. Why is CollectionUtils.isEmpty() preferred over calling isEmpty() directly?
Calling collection.isEmpty() directly assumes the reference itself is non-null; if collection is null - which happens often with optional fields, method parameters, or values pulled from external data sources - that call throws a NullPointerException before the emptiness check even runs. // fragi...
29. How does a TreeBag maintain element ordering internally?
TreeBag
30. How does LRUMap decide which entry to evict?
LRUMap tracks access recency internally using a doubly linked structure layered over its hash table, similar to how LinkedHashMap behaves in access-order mode - every get() or put() moves the touched entry to the "most recently used" end of that internal ordering. graph LR A[put/get called] --> B...
31. What is the difference between HashBag and TreeBag?
Both are core Bag implementations, but they trade off ordering guarantees against raw performance in opposite directions. HashBag TreeBag backed by a HashMap backed by a TreeMap no defined iteration order iterates in sorted order O(1) average add/remove/getCount O(log n) add/remove/getCount eleme...
32. Why do we use predicate chaining with allPredicate and anyPredicate?
Predicate chaining lets you compose several independent validation rules into a single reusable Predicate object, instead of hard-coding a chain of if / else checks wherever the filtering logic is needed. Predicate
33. How is UnmodifiableMap in Commons Collections different from java.util's Collections.unmodifiableMap?
Functionally, both wrap an existing Map and throw UnsupportedOperationException on any mutating call - put() , remove() , clear() - while still being a live read-through view: changes made to the original backing map are visible through the wrapper. Collections.unmodifiableMap() Commons Collectio...
34. What happens when you add a duplicate value to a BidiMap?
A BidiMap enforces that every value maps back to exactly one key, so inserting a value that's already associated with a different key doesn't create a second mapping - it silently removes the old mapping first. BidiMap < String, Integer > scores = new DualHashBidiMap <> (); scores.put( "alice" , ...
35. How does PredicatedList enforce validation on add operations?
PredicatedList
36. Explain the internal working of CircularFifoQueue?
CircularFifoQueue
37. How can you optimize repeated multi-field lookups using MultiKeyMap?
A common but inefficient pattern for combining two lookup keys is nesting maps: Map
38. What is the difference between Apache Commons Collections 3.x and 4.x?
Commons Collections 4 was a deliberate, breaking redesign of the 3.x line rather than an incremental update, mainly to fix generics-related design problems that couldn't be solved without changing method signatures. Commons Collections 3.x Commons Collections 4.x org.apache.commons.collections or...
39. 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...
40. When should you choose Apache Commons Collections over Guava collections?
Both libraries extend the JDK's collection APIs, but they emphasize different things, so the right choice depends on what a project already has and what specific structure it actually needs. Existing dependency - if a codebase already pulls in Commons Collections (common in older Spring or enterp...
41. Explain the execution flow of CollectionUtils.collect()?
CollectionUtils.collect(inputCollection, transformer) performs a functional "map" operation: it walks the input collection element by element, in iteration order, applying the given Transformer to each one, and appends every result to an output collection. graph TD A[Start: iterate input collecti...
42. How is FactoryUtils used to lazily create objects?
FactoryUtils wraps different object-creation strategies behind the single-method Factory> listFactory = FactoryUtils.instantiateFactory(ArrayList.class); Map...
43. Why is Apache Commons Collections associated with a well-known deserialization vulnerability?
In 2015, security researchers demonstrated that several of the library's reflective functor classes could be chained together to build a so-called "gadget chain" - a sequence of ordinary, individually harmless objects that, when deserialized in a specific combination, ends up executing arbitrary ...
44. What is the difference between the legacy MultiMap (3.x) and MultiValuedMap (4.x)?
In Commons Collections 3.x, MultiMap was defined by extending java.util.Map
45. How does ReferenceMap help prevent memory leaks in long-running caches?
A plain HashMap used as a cache holds a strong reference to every key and value it stores, which means nothing can ever be garbage collected until it's explicitly removed - if entries accumulate faster than they're evicted, the cache grows without bound and can eventually trigger an OutOfMemoryEr...
46. Why doesn't a Bag simply behave like a Set?
A Set 's contract is built entirely around uniqueness: calling add() with a value that's already present is a no-op that returns false , and the set's size only ever reflects distinct elements. That contract structurally cannot represent "how many times did this occur" - the information is thrown...
47. How do you troubleshoot a ConcurrentModificationException when using CollectionUtils.filter()?
CollectionUtils.filter(collection, predicate) mutates the collection in place : it walks the collection's own Iterator and calls Iterator.remove() on every element that fails the predicate. A ConcurrentModificationException (CME) shows up when something else disturbs that same collection while fi...
48. Explain the lifecycle of a ClosureUtils.chainedClosure() execution?
ClosureUtils.chainedClosure(closure1, closure2, ...) returns a single composite Closure that, when its execute(input) is called once, runs each wrapped closure in the order they were supplied, passing the same input object to every one of them in turn. graph TD A[chainedClosure.execute(input) cal...
49. What is the difference between SetUtils.union() and manually merging two sets?
SetUtils.union(setA, setB) returns a live, unmodifiable Set view that computes membership by delegating to the two original sets rather than eagerly copying every element into a new backing structure the moment it's called. Set
50. How does Commons Collections' Trie support prefix-based lookups?
The Trie