</>
Skip to content
Java lessons (40/47)

Java — Iterator

Basic iterator

import java.util.Iterator;

ArrayList<String> list = new ArrayList<>();
Iterator<String> iter = list.iterator();

while (iter.hasNext()) {
    String item = iter.next();
    System.out.println(item);
}

Remove with iterator

while (iter.hasNext()) {
    String item = iter.next();
    if (item.equals("Bob")) {
        iter.remove();
    }
}

Mini Practice

  1. Create iterator
  2. Traverse collection
  3. Remove elements
  4. Practice iterator

Up Next

Continue with Exceptions - Error handling.

Related Topics

Frequently Asked Questions about Iterator

What is Iterator in Java?

Iterator is a fundamental concept in Java. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Iterator?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Iterator.

Why is Iterator important in Java?

Iterator is essential for Java development. Understanding this concept will help you write better code and solve real-world problems more effectively.