Using of Iterator in Java

Java presents a standardized method for iterating over collection elements through its Iterator interface. This interface is integral to collections such as List, Set, and the like. Given that it's an interface, diverse implementations can emerge, yet they all promise a unified mechanism for element traversal. The beauty of the Iterator interface is its ability to mask the intricacies of the foundational data structure. As a result, regardless of whether you're interacting with an ArrayList, a HashSet, or any distinct collection type, the Iterator's functionalities can be leveraged uniformly. This consistency simplifies the process for developers, ensuring they can focus on their core tasks rather than the nuances of collection traversal. In this Java Iterator Example, we showed an example of working with an iterator. Click Execute to run the Java Iterator Example online and see the result.
Using of Iterator in Java Execute
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IteratorExample {

    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();

        Iterator<String> fruitIterator = fruits.iterator();

        while (fruitIterator.hasNext()) {
            String currentFruit =;

            if ("Banana".equals(currentFruit)) {

        System.out.println("List after iteration: " + fruits);
Updated: Viewed: 33 times