Prev Next

Web / Apache Commons Collections Interview questions

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 filter() is mid-iteration.

// risky: filtering while also iterating the same list elsewhere
for (String s : names) {
    if (s.isEmpty()) {
        CollectionUtils.filter(names, StringUtils::isNotBlank); // CME
    }
}

// safer: snapshot first, or use select() instead of filter()
List<String> snapshot = new ArrayList<>(names);
Collection<String> kept = CollectionUtils.select(snapshot, StringUtils::isNotBlank);

Common causes are: calling filter() from inside a for-each loop over the same reference, calling it concurrently from another thread without synchronization, or passing a fixed-size/immutable view whose iterator doesn't support remove() at all (which throws UnsupportedOperationException rather than CME, but is diagnosed the same way - check what kind of collection was actually passed in).

The fix is usually one of two things: copy the input into a fresh, independent collection before filtering if the original reference must stay untouched during the operation, or switch to CollectionUtils.select()/selectRejected(), which build and return a brand-new filtered collection instead of mutating the source - sidestepping the in-place removal that causes the CME in the first place.

CollectionUtils.filter() causes a CME typically when:
A safer alternative to in-place filter() when the original must stay untouched is:

More Related questions...

What is Apache Commons Collections? What are the main packages in Apache Commons Collections 4? What is a Bag in Apache Commons Collections? What are the types of Bag implementations in Commons Collections? What is a BidiMap in Apache Commons Collections? What is a MultiValuedMap in Apache Commons Collections? What is the purpose of CollectionUtils in Apache Commons Collections? What is the purpose of MapUtils in Apache Commons Collections? What is the purpose of ListUtils in Apache Commons Collections? What are Predicates in Apache Commons Collections? What are Transformers in Apache Commons Collections? What are Closures in Apache Commons Collections? What is a Factory in Apache Commons Collections? Define CircularFifoQueue in Apache Commons Collections? What is an LRUMap in Apache Commons Collections? What is a ReferenceMap in Apache Commons Collections? What is a MultiKeyMap in Apache Commons Collections? What is the purpose of IteratorUtils in Apache Commons Collections? Describe the LoopingIterator class in Apache Commons Collections? What is an OrderedMap in Apache Commons Collections? What is a SortedBidiMap in Apache Commons Collections? What is the purpose of ComparatorUtils in Apache Commons Collections? Define FixedOrderComparator in Apache Commons Collections? What is a PredicatedCollection in Apache Commons Collections? What is a TransformedCollection in Apache Commons Collections? What is the difference between a Map and a MultiValuedMap? What is the difference between a BidiMap and a regular Map? Why is CollectionUtils.isEmpty() preferred over calling isEmpty() directly? How does a TreeBag maintain element ordering internally? How does LRUMap decide which entry to evict? What is the difference between HashBag and TreeBag? Why do we use predicate chaining with allPredicate and anyPredicate? How is UnmodifiableMap in Commons Collections different from java.util's Collections.unmodifiableMap? What happens when you add a duplicate value to a BidiMap? How does PredicatedList enforce validation on add operations? Explain the internal working of CircularFifoQueue? How can you optimize repeated multi-field lookups using MultiKeyMap? What is the difference between Apache Commons Collections 3.x and 4.x? Why should you use TransformedMap instead of manual validation in setters? When should you choose Apache Commons Collections over Guava collections? Explain the execution flow of CollectionUtils.collect()? How is FactoryUtils used to lazily create objects? Why is Apache Commons Collections associated with a well-known deserialization vulnerability? What is the difference between the legacy MultiMap (3.x) and MultiValuedMap (4.x)? How does ReferenceMap help prevent memory leaks in long-running caches? Why doesn't a Bag simply behave like a Set? How do you troubleshoot a ConcurrentModificationException when using CollectionUtils.filter()? Explain the lifecycle of a ClosureUtils.chainedClosure() execution? What is the difference between SetUtils.union() and manually merging two sets? How does Commons Collections' Trie support prefix-based lookups?
Show more question and Answers...


Comments & Discussions