← Back to Home

Module 1: Iterators

Learning Objectives

Introduction to Iterators

Iterators are a fundamental design pattern in Java that provide a way to access elements of a collection sequentially without exposing its underlying representation. The Iterator pattern allows for different traversal techniques and provides a common interface for traversing different types of collections.

In Java, the Iterator interface is part of the Java Collections Framework and provides methods to iterate through a collection, check if there are more elements, and retrieve the next element.

Iterators are particularly useful because they give you the ability to safely add and remove elements while iterating through a collection. Without iterators, attempting to modify a collection during a for-each loop would result in a ConcurrentModificationException.

Iterator Methods

The Iterator interface includes three key methods that help you traverse a collection:

ListIterator

The ListIterator<E> extends the functionality of Iterator<E> when working with List<E> classes. It provides bidirectional traversal and the ability to add and update elements in addition to the standard iterator operations.

Additional methods in ListIterator include:

Key Topics

Iterator Interface

Learn about the Java Iterator interface and its core methods.

  • hasNext() method
  • next() method
  • remove() method

Implementing Custom Iterators

Learn how to create custom Iterator implementations for your own data structures.

  • Creating an Iterator class
  • Managing state during iteration
  • Handling edge cases

Iterable Interface

Understand the Iterable interface and how it relates to Iterator.

  • The iterator() method
  • Enhanced for loops
  • Making your classes Iterable

Iterator Use Cases

Explore common scenarios where Iterators are useful in real-world applications.

  • Traversing collections
  • Stream operations
  • Data processing

Practical Iterator Examples

Traversing Collections with Iterators

You can iterate over a list using either a for loop or a while loop. Here's what it looks like:

// Using a for loop with ListIterator
for (ListIterator<String> iterator = list.listIterator(); iterator.hasNext();) {
    System.out.println(iterator.next() + " ");
}

// Using a while loop with ListIterator
ListIterator<String> iterator = list.listIterator();
while (iterator.hasNext()) {
    System.out.print(iterator.next() + " ");
}

Manipulating Lists with ListIterator

Here are examples of using ListIterator to add, update, and remove elements from a list:

Adding Elements

List<String> groceryList = new ArrayList<>();
ListIterator<String> groceryIterator = groceryList.listIterator();
groceryIterator.add("apples");
groceryIterator.add("bananas");
// List now contains: apples bananas

Updating Elements

ListIterator<String> groceryIterator = groceryList.listIterator();
if (groceryIterator.hasNext()) {
    groceryIterator.next();      // returns "apples"
    groceryIterator.set("oranges");
}
// List now contains: oranges bananas

Removing Elements

ListIterator<String> groceryIterator = groceryList.listIterator();
if (groceryIterator.hasNext()) {
    groceryIterator.next();      // returns the first element
    groceryIterator.remove();
}
// First element has been removed

Common Pitfalls to Avoid

Resources

Iterators Code-Along Starter

Starter code for the iterators code-along exercise.

Iterators Code-Along Solution

Solution code for the iterators code-along exercise.

Code-Alongs

Additional code-along exercises for this sprint.

Sprint Challenge

Access the sprint challenge for this unit.