Introduction
The Iterator interface in Java is a part of the java.util package.
It provides a way to traverse a collection of elements one by one.
It is a fundamental part of the Java Collections Framework and is used to retrieve elements sequentially from a collection.
Table of Contents
- What is the
IteratorInterface? - Common Methods
- Examples of Using the
IteratorInterface - Conclusion
1. What is the Iterator Interface?
The Iterator interface provides methods to iterate over a collection. It helps to access elements of a collection sequentially without exposing the underlying representation. Iterators are used to remove elements from the underlying collection during the iteration.
2. Common Methods
boolean hasNext(): Returnstrueif the iteration has more elements.E next(): Returns the next element in the iteration.void remove(): Removes the last element returned by the iterator.
3. Examples of Using the Iterator Interface
Example 1: Basic Usage of Iterator
This example demonstrates how to use an Iterator to traverse a List.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String fruit = iterator.next();
System.out.println(fruit);
}
}
}
Output:
Apple
Banana
Cherry
Example 2: Removing Elements Using Iterator
This example shows how to remove elements from a collection using an Iterator.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class RemoveExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String fruit = iterator.next();
if ("Banana".equals(fruit)) {
iterator.remove();
}
}
System.out.println("List after removal: " + list);
}
}
Output:
List after removal: [Apple, Cherry]
Example 3: Using Iterator with a Set
This example demonstrates how to use an Iterator to traverse a Set.
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetIteratorExample {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Red");
set.add("Green");
set.add("Blue");
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String color = iterator.next();
System.out.println(color);
}
}
}
Output:
Red
Blue
Green
Example 4: Using Iterator with a Map
This example shows how to use an Iterator to traverse a Map.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class MapIteratorExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);
Iterator<Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, Integer> entry = iterator.next();
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Output:
Apple: 1
Cherry: 3
Banana: 2
Example 5: Using ListIterator
This example demonstrates how to use a ListIterator to traverse a List in both directions.
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIteratorExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("One");
list.add("Two");
list.add("Three");
ListIterator<String> listIterator = list.listIterator();
System.out.println("Forward traversal:");
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
System.out.println("Backward traversal:");
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
}
Output:
Forward traversal:
One
Two
Three
Backward traversal:
Three
Two
One
4. Conclusion
The Iterator interface in Java provides a convenient way to traverse collections sequentially. It allows for safe removal of elements during iteration and ensures that the underlying collection’s structure is not exposed. The examples provided demonstrate common usage patterns and highlight the capabilities of the Iterator interface.