Lesson content
Read, practise, then check your understanding
Iterable supplies an Iterator, enabling enhanced for. An iterator is a cursor; call hasNext before next. Many iterators are fail-fast on unsupported structural changes, but this is bug detection rather than a concurrency guarantee.
Controlled removal
Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
if (iterator.next().isBlank()) {
iterator.remove();
}
}
Removing through the iterator keeps its expected modification state synchronized. ListIterator moves both directions and supports add, set, and index queries. forEachRemaining consumes remaining elements. Spliterator reports characteristics and can partition traversal for stream pipelines. Do not reuse exhausted iterators, assume stable order from unordered collections, or modify a collection concurrently without its documented mechanism. Prefer collection bulk methods or streams when manual cursor control adds no value.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.