Introduction to iterators

Introduction to Iterators

Iterators are a fundamental concept in programming that allow traversal through a collection of elements, one element at a time, without exposing the underlying implementation of the collection. Iterators provide a uniform way to access elements across different types of collections, making code more modular and easier to maintain. In this introduction, we’ll explore what iterators are, how they work, and why they are important in programming.

What is an Iterator?

An iterator is an object that enables a programmer to traverse through a collection, such as an array, list, set, or map, and access elements sequentially. It abstracts the process of iteration, allowing the consumer of the iterator to focus on processing each element rather than worrying about the internal structure of the collection.

How Iterators Work

Iterators typically provide two main operations:

Iteration: It allows sequential access to elements in the collection, one at a time, starting from the beginning and moving towards the end.

Element Retrieval: It provides a way to retrieve the current element being pointed to by the iterator.

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

Type is the data type of elements in the collection.

collection is any Collection object (e.g., ArrayList, HashSet, etc.).

iterator() is a method that returns an Iterator.Code language: HTML, XML (xml)

Common Methods of Iterator

Method Description
boolean hasNext() Returns true if the iteration has more elements.
E next() Returns the next element in the iteration.
void remove() Removes the last element returned by the iterator (optional operation).
import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListIteratorExample {
    public static void main(String[] args) {
        ArrayList<String> students = new ArrayList<>();
        students.add("Paani");
        students.add("Mahesh");
        students.add("Datta");
        students.add("Ganesh");
        students.add("Harsha");

        // Using Iterator to iterate over ArrayList
        Iterator<String> iterator = students.iterator();
        while (iterator.hasNext()) {
            String student = iterator.next();
            System.out.println(student);
        }
    }
}
/*
D:\>javac ArrayListIteratorExample.java

D:\>java ArrayListIteratorExample
Paani
Mahesh
Datta
Ganesh
Harsha
*/
import java.util.LinkedList;
import java.util.Iterator;

public class LinkedListIteratorExample {
    public static void main(String[] args) {
        LinkedList<String> students = new LinkedList<>();
        students.add("Paani");
        students.add("Mahesh");
        students.add("Datta");
        students.add("Ganesh");
        students.add("Harsha");

        // Using Iterator to iterate over LinkedList
        Iterator<String> iterator = students.iterator();
        while (iterator.hasNext()) {
            String student = iterator.next();
            System.out.println(student);
        }
    }
}

/*
D:\>javac LinkedListIteratorExample.java

D:\>java LinkedListIteratorExample
Paani
Mahesh
Datta
Ganesh
Harsha
*/
import java.util.Stack;
import java.util.Iterator;

public class StackIteratorExample {
    public static void main(String[] args) {
        Stack<String> students = new Stack<>();
        students.push("Paani");
        students.push("Mahesh");
        students.push("Datta");
        students.push("Ganesh");
        students.push("Harsha");

        // Using Iterator to iterate over Stack
        Iterator<String> iterator = students.iterator();
        while (iterator.hasNext()) {
            String student = iterator.next();
            System.out.println(student);
        }
    }
}

/*
D:\>javac StackIteratorExample.java

D:\>java StackIteratorExample
Paani
Mahesh
Datta
Ganesh
Harsha
*/

Iterators in Java are essential tools for iterating through collections of elements like the student names in our examples. They simplify the process of accessing and processing elements while maintaining encapsulation and promoting code reusability. Understanding iterators enables developers to efficiently manipulate data structures and write more concise and readable code in Java applications.

Scroll to Top