Learn Java
import com.learnjava.datastructurescollections.IteratingCollections;

Iterating Collections in Java

@Concept

Every standard collection can be walked in more than one way, and the differences matter once modification enters the picture. A for-each loop is the simplest option for read-only iteration; calling an explicit Iterator manually allows safely removing elements mid-traversal through the iterator's own remove() method, something a for-each loop cannot do safely at all; and forEach(lambda), from Java 8 onward, applies an action to every element in one expression.

Modifying a collection's structure — adding or removing elements — through any means other than the active iterator's own remove() method while a for-each or Iterator loop is in progress throws a ConcurrentModificationException. That's a safety check the collection performs specifically to catch this class of bug early, rather than let it corrupt state silently.

@HowItWorks
1

for-each for read-only iteration

The simplest option whenever elements just need to be visited, not removed.

2

Iterator, explicitly

Obtained via collection.iterator(), offering hasNext() and next() manually, plus a genuinely safe remove() during traversal.

3

Safe removal during iteration

Only the active iterator's own remove() method is safe to call mid-loop; calling the collection's remove() directly during a for-each is not.

4

ConcurrentModificationException

Thrown when a collection's structure changes during iteration through any means besides that iterator's own remove(), catching the bug immediately rather than letting it corrupt state silently.

5

forEach(lambda)

Applies a given action to every element in one expression, a concise alternative to a full loop when nothing needs to be removed.

@Example
import java.util.ArrayList;
import java.util.Iterator;
 
public class IterateDemo {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
 
Iterator<Integer> it = numbers.iterator();
while (it.hasNext()) {
if (it.next() == 2) {
it.remove();
}
}
System.out.println(numbers);
}
}
Output
[1, 3]
1The Iterator is obtained explicitly from the list, rather than using a for-each loop, specifically because an element needs to be removed mid-traversal.
2it.next() advances the iterator and returns each element in turn, checked against 2.
3When the element equal to 2 is found, it.remove() safely removes it through the iterator itself, which a for-each loop has no way to do at all.
4The single println call shows the list afterward, missing exactly the element that was removed.
@UseCases
Removing specific elements from a list while walking through it, safely
Debugging a ConcurrentModificationException caused by modifying a list during a for-each loop
Applying the same action to every element concisely with forEach and a lambda
Choosing between a for-each loop, an explicit Iterator, and forEach based on whether removal is needed

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.