Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Java

Iterator in Java

Traverse collections safely with Iterator and enhanced for-loops.

Iterator in Java — a practical guide to Java Iterator with clear examples you can reuse in real projects.

Java Tutorial Series (57/90). Prefer one article? Read the complete Java tutorial.

Short description

Use Iterator when you need to remove elements during traversal.

Iterator remove

import java.util.*;

List<String> list = new ArrayList<>(List.of("a", "b", "c"));
Iterator<String> it = list.iterator();
while (it.hasNext()) {
    if (it.next().equals("b")) it.remove();
}

Leave a reply

Your email address will not be published. Required fields are marked *