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.
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.
1import java.util.ArrayList;2import java.util.Iterator;34public class Main {5public static void main(String[] args) {6ArrayList<String> list = new ArrayList<>();7list.add("Apple");8list.add("Banana");9list.add("Cherry");1011Iterator<String> iterator = list.iterator();12while (iterator.hasNext()) {13System.out.println(iterator.next());14};15}16}
Apple Banana Cherry
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.
1import java.util.ArrayList;2import java.util.Iterator;34public class Main {5public static void main(String[] args) {6ArrayList<String> list = new ArrayList<>();7list.add("Apple");8list.add("Banana");9list.add("Cherry");1011Iterator<String> iterator = list.iterator();12while (iterator.hasNext()) {13String fruit = iterator.next();14System.out.println(fruit);15}16}17}
Apple Banana Cherry
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.
1import java.util.ArrayList;2import java.util.Iterator;34public class Main {5public static void main(String[] args) {6ArrayList<String> list = new ArrayList<>();7list.add("Apple");8list.add("Banana");9list.add("Cherry");1011Iterator<String> iterator = list.iterator();12while (iterator.hasNext()) {13String fruit = iterator.next();14if (fruit.equals("Banana")) {15iterator.remove();16}17}1819System.out.println(list);20}21}
[Apple, Cherry]
next() Without Checking hasNext(): This can lead to a NoSuchElementException.remove() More Than Once Consecutively: This will throw an IllegalStateException.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.
1import java.util.ArrayList;2import java.util.Iterator;34public class Main {5public static void main(String[] args) {6ArrayList<Integer> numbers = new ArrayList<>();7for (int i = 1; i <= 10; i++) {8numbers.add(i);9}1011Iterator<Integer> iterator = numbers.iterator();12while (iterator.hasNext()) {13int number = iterator.next();14if (number % 2 == 0) {15iterator.remove();16}17}1819System.out.println("Odd numbers: " + numbers);20}21}
Odd numbers: [1, 3, 5, 7, 9]
| Concept | Description |
|---|---|
| Iterator | An 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 Mistakes | Calling next() without hasNext(), modifying collections directly, and calling remove() consecutively. |
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!