Iteration semantics
16 questions in 4 subtopics.
Iterator and ListIterator
What can aListIterator do that an Iterator cannot, and where is its cursor positioned?This code throws ConcurrentModificationException:for (String s : names) {
if (s.isBlank()) names.remove(s);
}
ArrayList. Show two correct ways to remove blank names.You want to expose a repository method that returns all rows of a 50-million-row table for export without loading them into memory. Compare returning an Iterator, an Iterable, and a Stream, considering resource cleanup.A platform library offers an Iterator<Record> over paginated results from a remote API for many services. Teams report missing or duplicated records when data changes during iteration, and leaked HTTP connections when they stop early. Design the iteration contract.Iterator has no close method and separate hasNext and next calls, while Spliterator uses a single tryAdvance and reports characteristics, and BaseStream implements AutoCloseable. What lessons do these design differences teach for long-lived iteration APIs over remote or resource-backed data?Fail-fast and fail-safe
Explain fail-fast iterators and the alternative behaviour of iterators fromCopyOnWriteArrayList and ConcurrentHashMap.A reporting endpoint iterates a ConcurrentHashMap of 2 million account balances to compute a total while transfers update balances concurrently. Finance reports totals that do not match. Explain and propose a fix.A shared library used by 30 services exposes getSubscribers() returning its internal ArrayList while a background thread updates it. Services get intermittent ConcurrentModificationException and occasional wrong results. You own the library. What contract do you define and how do you change it without breaking users?Fail-fast iterators detect misuse early but only on a best-effort basis, while weakly consistent iterators never fail but may miss updates. What broader principle on failing fast versus degrading would you set for your platform's libraries and services, and where is weak consistency acceptable?ConcurrentModificationException
A single-threaded event bus iterates its listeners list and calls each listener; one listener unregisters itself during the callback. The application throwsConcurrentModificationException even though there is only one thread. Explain and fix.Why does this code NOT throw ConcurrentModificationException, and why is it still a bug?List<String> l = new ArrayList<>(List.of("a", "b", "c"));
for (String s : l) {
if (s.equals("b")) l.remove(s);
}
System.out.println(l);
ArrayList occasionally fails with ConcurrentModificationException while request threads add to the list. A teammate proposes catching the exception and retrying. What do you say?After a shared library upgrade, 15 services start throwing ConcurrentModificationException at a low rate in production. You are leading the incident. Describe your actions during the incident and afterwards.ForEach and removeIf
CompareIterable.forEach with an enhanced for loop, and explain why ArrayList.removeIf is better than removing in a loop.Code iterates a Collections.synchronizedList with an enhanced for loop and occasionally throws ConcurrentModificationException, but list.forEach(...) never does. Explain, and show how to iterate correctly.Java moved from Enumeration (1.0) to fail-fast Iterator (1.2) to internal iteration with forEach, Spliterator and streams (8), and added Stream Gatherers in Java 24. Why did each step happen, and what does internal iteration let the platform do that external iteration could not?No question matches these choices.