Java Iterator
Java iterator is an interface that provides a standardized way to traverse elements in a collection sequentially, one element at a time. It acts as a cursor, meaning a pointer that moves through the collection and keeps track of the current position during iteration. This allows access to elements individually without exposing the internal structure of the collection.
The iterator can be used with collections such as ArrayList, LinkedList, HashSet, and other classes that implement the Collection interface. It is part of the Java Collections Framework and is found in the java.util package.
Syntax of Java iterator
To create an iterator, use the following syntax:
Iterator<E> iteratorName = collection.iterator();
Parameters:
collection: Any object that implements theCollectioninterfaceE: The type of elements stored in the collection
Return value:
Returns an Iterator that can traverse the collection one element at a time
Key methods:
hasNext(): Returnstrueif there are more elements to iterate overnext(): Returns the next element in the iteration and advances the cursorremove(): Removes the last element returned by the iterator. Not all iterators support this operation.
Example 1: Basic Java Iterator Usage
This example demonstrates the fundamental usage of an iterator to traverse through an ArrayList:
import java.util.ArrayList;import java.util.Iterator;public class BasicIteratorExample {public static void main(String[] args) {// Create an ArrayList and add elementsArrayList<String> fruits = new ArrayList<>();fruits.add("Apple");fruits.add("Banana");fruits.add("Cherry");fruits.add("Date");// Create an iterator for the ArrayListIterator<String> it = fruits.iterator();// Iterate through the collectionwhile (it.hasNext()) {String fruit = it.next();System.out.println(fruit);}}}
The output of the above code is:
AppleBananaCherryDate
This example creates an ArrayList of fruits, obtains an iterator using the iterator() method, and uses a while loop with hasNext() and next() methods to traverse and print each element.
Example 2: Filtering Values in a List Using Java Iterators
This example shows how to use an iterator to filter elements based on a condition:
import java.util.ArrayList;import java.util.Iterator;public class FilterExample {public static void main(String[] args) {ArrayList<Integer> numbers = new ArrayList<>();numbers.add(10);numbers.add(25);numbers.add(40);numbers.add(55);Iterator<Integer> it = numbers.iterator();System.out.println("Numbers greater than 30:");while (it.hasNext()) {int num = it.next();if (num > 30) {System.out.println(num);}}}}
The output of the above code is:
Numbers greater than 30:4055
This example demonstrates using an iterator to filter numbers from a list, displaying only those greater than 30.
Example 3: Using Java Iterators To Remove Elements During Iteration
This example shows how to use iterator.remove() to safely remove matching elements from a list during iteration:
import java.util.ArrayList;import java.util.Iterator;public class RemoveExample {public static void main(String[] args) {ArrayList<String> items = new ArrayList<>();items.add("Keep");items.add("Remove");items.add("Keep");Iterator<String> it = items.iterator();while (it.hasNext()) {String item = it.next();if (item.equals("Remove")) {it.remove(); // Safe removal}}System.out.println("Updated list: " + items);}}
The output of the above code is:
Updated list: [Keep, Keep]
Frequently Asked Questions
1. What is the difference between for loop and iterator?
A traditional for loop uses index access and is limited to indexed collections. An iterator works with all collection types and provides safe element removal during traversal, while modifying collections during for loops can cause issues.
2. What is the alternative to iterator in Java?
The main alternatives include enhanced for loops (for-each), traditional for loops, streams, and ListIterator. Enhanced for loops are the most common alternative for simple traversal.
3. What is the purpose of an iterator?
An iterator provides a standard, safe way to traverse collections without exposing their internal structure. It enables sequential access and supports safe modification during iteration across all collection types.
Iterator
- .add()
- Adds an entry to the underlying collection of a ListIterator object.
- .hasNext()
- Returns true if an Iterator or a ListIterator object has more elements. Returns false otherwise.
- .hasPrevious()
- Returns true if a ListIterator object has prior elements. Returns false otherwise.
- .next()
- Returns the next element from an Iterator or a ListIterator object.
- .nextIndex()
- Returns the index of next element from a ListIterator object.
- .previous()
- Returns the previous element from a ListIterator object.
- .previousIndex()
- Returns the index of the previous element from a ListIterator object.
- .remove()
- Removes an entry from the underlying collection of an Iterator or ListIterator object.
- .set()
- Replaces the current entry in the underlying collection of a ListIterator object.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours