Java Iterator
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Iterator

Harine

Java Iterator

What is a Java Iterator?

In Java, an Iterator is an object used to traverse elements in a collection such as ArrayList, HashSet, or LinkedList. It provides a standard way to access elements sequentially without exposing the underlying structure of the collection.

The term iteration refers to the process of looping through elements one by one.

To use an Iterator, you must import it from the java.util package:

import java.util.Iterator;

How to Get an Iterator in Java

You can obtain an iterator from any collection by calling the iterator() method.

Example: Accessing Elements Using Iterator

import java.util.ArrayList;

import java.util.Iterator;

public class IteratorExample {

   public static void main(String[] args) {

     ArrayList<String> fruits = new ArrayList<>();

     fruits.add("Apple");

     fruits.add("Mango");

     fruits.add("Banana");

     fruits.add("Orange");

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

     // Access first element

     if (iterator.hasNext()) {

     System.out.println("First item: " + iterator.next());

    }

  }

}

Iterating Through a Collection

The Iterator interface provides two key methods:

hasNext() → checks if more elements are available

next() → returns the next element

Example: Looping Through All Elements

import java.util.ArrayList;

import java.util.Iterator;

public class IterateCollection {

   public static void main(String[] args) {

      ArrayList<String> cities = new ArrayList<>();

      cities.add("Chennai");

      cities.add("Delhi");

      cities.add("Mumbai");

      cities.add("Bangalore");

      Iterator<String> iterator = cities.iterator();

      while (iterator.hasNext()) {

         String city = iterator.next();

         System.out.println(city);

       }

     }

  }

Removing Elements Using Iterator

One of the major advantages of using an Iterator is that it allows safe removal of elements during iteration.

The remove() method deletes the current element returned by next().

Example: Remove Even Numbers

import java.util.ArrayList;

import java.util.Iterator;

public class RemoveExample {

   public static void main(String[] args) {

   ArrayList<Integer> values = new ArrayList<>();

   values.add(11);

   values.add(20);

   values.add(35);

   values.add(40);

   values.add(55);

Iterator<Integer> iterator = values.iterator();

while (iterator.hasNext()) {

  int num = iterator.next();

  if (num % 2 == 0) {

     iterator.remove(); // Safe removal

    }

  }

  System.out.println("After removal: " + values);

  }

}

Using var with Iterator (Java 10+)

From Java 10 onwards, you can use the var keyword to simplify iterator declarations.

Example: Using var

import java.util.ArrayList;

public class VarIteratorExample {

   public static void main(String[] args) {

      ArrayList<String> languages = new ArrayList<>();

      languages.add("Java");

      languages.add("Python");

      languages.add("C++");

      var iterator = languages.iterator();

      while (iterator.hasNext()) {

          System.out.println(iterator.next());

       }

    }

  }

Why Use var?

  • Reduces verbosity
  • Improves readability in simple cases
  • Compiler still infers the correct type (Iterator<String>)

However, many developers prefer explicit types for better clarity in large codebases.

Key Benefits of Using Iterator

  • Provides a uniform way to traverse different collections
  • Supports safe removal of elements
  • Works with all classes in the Java Collection Framework
  • Helps avoid runtime errors when modifying collections

Our website uses cookies to enhance your experience. Learn More
Accept !