Iterators & Enhanced for
Traverse collections with Iterator and enhanced for. Learn safe removal during iteration and common pitfalls.
Iterators & Enhanced for is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Overview
Iterator gives step-by-step control to walk through a collection. Enhanced for (for-each) is shorter and clean for reading elements. Use Iterator when you need to remove safely during traversal.
Iterator vs for-each
- Iterator: use when you may remove items or need fine control.
- Enhanced for: best for read-only loops.
- Never structurally modify the collection inside enhanced for.
Code: basic Iterator
Iterator uses hasNext() and next(). Each call to next() moves forward and returns the element.
public class Main {
public static void main(String[] args) {
java.util.List<String> names = new java.util.ArrayList<>();
names.add("Ada"); names.add("Bob"); names.add("Cyd");
java.util.Iterator<String> it = names.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
}
Pitfall
Pitfall: Removing directly inside an enhanced for loop often throws ConcurrentModificationException. Use Iterator for safe structural changes.
Code: safe removal
Safe removal: call remove() on the Iterator right after next() when a match is found.
public class Main {
public static void main(String[] args) {
java.util.List<String> words = new java.util.ArrayList<>();
words.add("to"); words.add("be"); words.add("or"); words.add("not");
java.util.Iterator<String> it = words.iterator();
while (it.hasNext()) {
String w = it.next();
if (w.length() <= 2) {
it.remove(); // safe removal
}
}
System.out.println(words); // [not]
}
}
Code: enhanced for/forEach
Enhanced for is clean for reading. forEach with a lambda is another short style (Java 8+).
public class Main {
public static void main(String[] args) {
java.util.Set<String> tags = new java.util.HashSet<>();
tags.add("java"); tags.add("collections"); tags.add("oop");
// enhanced for: read-only
for (String t : tags) {
System.out.print("[" + t + "]");
}
System.out.println();
// Java 8+ forEach
tags.forEach(t -> System.out.println("tag=" + t));
}
}
Iterator removal check
Quick check: How do you safely remove elements while iterating a List?
Recap
Recap: Use enhanced for for simple reads. Use Iterator when you must remove items during traversal. This prevents runtime errors.
Frequently asked questions
Is the “Iterators & Enhanced for” lesson free?
Yes — the full text of “Iterators & Enhanced for” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Iterators & Enhanced for”?
Traverse collections with Iterator and enhanced for. Learn safe removal during iteration and common pitfalls. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Iterators & Enhanced for” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- List & Set Overview
- Iterators & Enhanced for
- Collections Utilities