Ugenz Academy/Tutorial

Iterators

4 min read769 words

Reading progress

Keep learning

Iterators in Java

Introduction

An Iterator in Java is an object used to traverse or access elements of a collection one by one.

It is mainly used with collections such as ArrayList, HashSet, and other classes that implement the Collection interface.

The Iterator interface is available in the java.util package.

import java.util.Iterator;

Why Use an Iterator?

An Iterator is useful when we want to:

  • Access collection elements one by one.
  • Traverse a collection.
  • Remove elements safely while iterating.
  • Work with different types of collections using a common approach.

Iterator Syntax

Iterator<Type> iteratorName = collection.iterator();

Example:

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

Basic Iterator Example

import java.util.*; public class IteratorExample { public static void main(String[] args) { List<String> names = new ArrayList<>(); names.add("Anu"); names.add("Ram"); names.add("Sita"); Iterator<String> iterator = names.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } }

Output

Anu Ram Sita

Important Iterator Methods

MethodDescription
hasNext()Checks whether another element exists
next()Returns the next element
remove()Removes the current element

1. hasNext()

The hasNext() method checks whether there is another element in the collection.

if (iterator.hasNext()) { System.out.println("Element exists"); }

It returns:

true

or

false

2. next()

The next() method returns the next element.

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

For example:

Anu Ram Sita

3. remove()

The remove() method removes the last element returned by next().

Example:

import java.util.*; public class IteratorRemove { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(30); Iterator<Integer> iterator = numbers.iterator(); while (iterator.hasNext()) { int number = iterator.next(); if (number == 20) { iterator.remove(); } } System.out.println(numbers); } }

Output

[10, 30]

Iterator with Set

An Iterator can also be used with a Set.

import java.util.*; public class SetIterator { public static void main(String[] args) { Set<String> names = new HashSet<>(); names.add("Anu"); names.add("Ram"); names.add("Sita"); Iterator<String> iterator = names.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } }

The order may vary because HashSet does not guarantee insertion order.

Iterator with List

List<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(30); Iterator<Integer> iterator = numbers.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }

Output

10 20 30

Iterator vs Enhanced For Loop

IteratorEnhanced For Loop
Uses Iterator objectUses for-each syntax
Can safely remove elements using remove()Direct removal during iteration can cause problems
Provides hasNext() and next()Simpler syntax
Useful when more control is requiredUseful for simple traversal

Enhanced For Loop

for (String name : names) { System.out.println(name); }

Iterator

Iterator<String> iterator = names.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }

Iterator vs ListIterator

Java provides another interface called ListIterator.

IteratorListIterator
Works with CollectionMainly works with List
Moves forwardMoves forward and backward
Has hasNext()Has hasNext() and hasPrevious()
Supports remove()Supports remove()
Cannot add elements during iterationCan add elements using add()

Example:

List<String> names = new ArrayList<>(); names.add("Anu"); names.add("Ram"); ListIterator<String> iterator = names.listIterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }

Important Points

  • Iterator is an interface in java.util.
  • It is used to traverse collections.
  • hasNext() checks for the next element.
  • next() returns the next element.
  • remove() removes the current element.
  • Iterator generally moves in the forward direction.
  • ListIterator provides forward and backward traversal.
  • Iterator is useful with collections such as List and Set.

Advantages of Iterator

  1. Provides a standard way to traverse collections.
  2. Works with different collection types.
  3. Allows safe removal during iteration.
  4. Simple and easy to use.
  5. Provides better control than a simple loop when modifying a collection.

Real-Life Example

Suppose a teacher has a list of students:

Anu Ram Sita Hari

An Iterator can visit each student one by one:

Anu → Ram → Sita → Hari

This is similar to moving through a collection element by element.

Conclusion

An Iterator is a useful Java interface for traversing collection elements one by one. It provides methods such as hasNext(), next(), and remove(). It is especially useful when we need controlled traversal or safe removal of elements during iteration.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy