codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

55 / 65 topics
50Java Collections51Java ArrayList52Java LinkedList53Java HashSet54Java HashMap55Java Iterator
Tutorials/Java Programming/Java Iterator
☕Java Programming

Java Iterator

Updated 2026-05-12
15 min read

Java Iterator

Introduction

In Java, an iterator is a design pattern that provides a way to access the elements of a collection, such as lists or sets, without exposing its underlying structure. Iterators are particularly useful when you need to traverse collections while performing operations like adding or removing items safely. This tutorial will guide you through understanding and using iterators in Java.

Core Content

What is an Iterator?

An iterator is an object that enables you to traverse a collection of elements sequentially, without needing to know the details of its implementation. It provides two main methods: hasNext() and next(). The hasNext() method checks if there are more elements in the collection, while the next() method retrieves the next element.

Example 1: Basic Iterator Usage

Java
1import java.util.ArrayList;
2import java.util.Iterator;
3
4public class Main {
5 public static void main(String[] args) {
6 ArrayList<String> list = new ArrayList<>();
7 list.add("Apple");
8 list.add("Banana");
9 list.add("Cherry");
10
11 Iterator<String> iterator = list.iterator();
12 while (iterator.hasNext()) {
13 System.out.println(iterator.next());
14 };
15 }
16}
Output
Apple
Banana
Cherry

How to Use hasNext() and next()

The hasNext() method returns a boolean value indicating whether there are more elements in the collection. The next() method retrieves the next element from the collection.

Example 2: Using hasNext() and next()

Java
1import java.util.ArrayList;
2import java.util.Iterator;
3
4public class Main {
5 public static void main(String[] args) {
6 ArrayList<String> list = new ArrayList<>();
7 list.add("Apple");
8 list.add("Banana");
9 list.add("Cherry");
10
11 Iterator<String> iterator = list.iterator();
12 while (iterator.hasNext()) {
13 String fruit = iterator.next();
14 System.out.println(fruit);
15 }
16 }
17}
Output
Apple
Banana
Cherry

Removing Items Safely During Iteration

Iterators provide a safe way to remove elements from a collection during iteration. The remove() method can be called on the iterator to delete the last element returned by the next() method.

Example 3: Removing Elements Using Iterator

Java
1import java.util.ArrayList;
2import java.util.Iterator;
3
4public class Main {
5 public static void main(String[] args) {
6 ArrayList<String> list = new ArrayList<>();
7 list.add("Apple");
8 list.add("Banana");
9 list.add("Cherry");
10
11 Iterator<String> iterator = list.iterator();
12 while (iterator.hasNext()) {
13 String fruit = iterator.next();
14 if (fruit.equals("Banana")) {
15 iterator.remove();
16 }
17 }
18
19 System.out.println(list);
20 }
21}
Output
[Apple, Cherry]

Common Mistakes and Pitfalls

  1. Calling next() Without Checking hasNext(): This can lead to a NoSuchElementException.
  2. Modifying the Collection Directly While Iterating: Use iterators for safe modification.
  3. Calling remove() More Than Once Consecutively: This will throw an IllegalStateException.

Practical Example

Let's create a practical example where we use an iterator to filter out even numbers from a list and print the remaining odd numbers.

Java
1import java.util.ArrayList;
2import java.util.Iterator;
3
4public class Main {
5 public static void main(String[] args) {
6 ArrayList<Integer> numbers = new ArrayList<>();
7 for (int i = 1; i <= 10; i++) {
8 numbers.add(i);
9 }
10
11 Iterator<Integer> iterator = numbers.iterator();
12 while (iterator.hasNext()) {
13 int number = iterator.next();
14 if (number % 2 == 0) {
15 iterator.remove();
16 }
17 }
18
19 System.out.println("Odd numbers: " + numbers);
20 }
21}
Output
Odd numbers: [1, 3, 5, 7, 9]

Summary

ConceptDescription
IteratorAn object that enables sequential access to elements in a collection.
hasNext()Checks if there are more elements in the collection.
next()Retrieves the next element from the collection.
remove()Removes the last element returned by next() safely during iteration.
Common MistakesCalling next() without hasNext(), modifying collections directly, and calling remove() consecutively.

What's Next?

In the next topic, we will explore Java Wrapper Classes, which provide a way to use primitive data types as objects. This knowledge is essential for understanding more advanced concepts in Java, such as generics and collections.

Stay tuned!


PreviousJava HashMapNext Java Wrapper Classes

Recommended Gear

Java HashMapJava Wrapper Classes