Java collection exercises
Java collections are central to enterprise software, but common mistakes lead to silent bugs or runtime crashes: mutating an ArrayList while iterating over it, storing objects in a HashSet without overriding hashCode(), or modifying List.of() immutable lists.
Debug real Java collection pipelines and master the Java Collections Framework.
The collections bugs you will meet
- Modifying a collection inside an enhanced for-loop throws ConcurrentModificationException.
- Comparing Strings or wrapper objects with == compares references instead of contents; use .equals().
- Overriding equals() without overriding hashCode() breaks HashMap and HashSet lookups.
- List.of() and Arrays.asList() return fixed-size or immutable collections that throw UnsupportedOperationException on .add().
The exercises
Medium
- Unexpected runtime error during list removal in iterationAsync and race conditions
- HashMap Key Lookup Fails Due to Incorrect hashCode/equalsLogic
- Unexpected Mutation of Nested ArrayListsLogic
- List size issues when adding elements in JavaType
- Array Conversion Error in Generic Data ProcessingType
- Task results lost when ExecutorService tasks completeAsync and race conditions
- Cloned list elements exhibit unexpected shared stateLogic
- Unexpected null values during base class initializationNull and undefined
- Unintended Object Retention Causing Memory LeakScope and closure
- ConcurrentModificationException in ArrayList loopLogic
- HashMap lookup fails due to missing hashCodeLogic
- Stream toList produces unmodifiable list on mutationLogic
- PriorityQueue comparator violates transitivityLogic
- Raw type wildcard warning in generic list castingType
Errors you might be searching for
More JavaScript practice by topic
Questions
How do I safely remove elements while looping in Java?
Use Collection.removeIf(predicate) in modern Java, or obtain an explicit Iterator and call iterator.remove().