Java Iterator
The Java Iterator is a mechanism in the Java Collections Framework and an interface used to traverse collections (such as lists, sets, and maps, etc.).
It provides a unified way to access elements in a collection without needing to understand the specific implementation details of the underlying collection.
The Java Iterator is not a collection; it is a method for accessing collections, and can be used to iterate overArrayListandHashSetand other collections.
Iterator is the simplest implementation of the Java iterator. ListIterator is an interface in the Collection API that extends the Iterator interface.

The Iterator interface defines several methods. The three most commonly used are:
next()- Returns the next element of the iterator and moves the iterator's pointer to the next position.
hasNext()- Used to determine whether there is a next element in the collection that can be accessed.
remove()- Removes the last element accessed by the iterator from the collection (optional operation).
The Iterator class is located in the java.util package. You need to import it before use. The syntax is as follows:
import java.util.Iterator; // 引入 Iterator 类
By using an iterator, we can access elements in a collection one by one without using traditional for loops or indexes. This approach is more concise and flexible, and it works for various types of collections.
Getting an Iterator
To get an iterator from a collection, you can use the iterator() method:
Example
import java.util.ArrayList;
import java.util.Iterator;
public class ExampleTest {
public static void main(String[] args) {
// Create a collection
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("Example");
sites.add("Taobao");
sites.add("Zhihu");
// Get the iterator
Iterator<String> it = sites.iterator();
// Output the first element in the collection
System.out.println(it.next());
}
}
Executing the above code produces the following output:
When using an iterator to traverse a collection, if the collection is modified during traversal (for example, adding or removing elements), it may cause a ConcurrentModificationException. To avoid this problem, you can use the iterator's ownremove()method to perform deletion operations.
Looping Over Collection Elements
The simplest way to make the iterator it return all elements in the collection one by one is to use a while loop:
while(it.hasNext()) {
System.out.println(it.next());
}
The following outputs all elements in the collection sites:
Example
import java.util.ArrayList;
import java.util.Iterator;
public class ExampleTest {
public static void main(String[] args) {
// Create a collection
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("Example");
sites.add("Taobao");
sites.add("Zhihu");
// Get the iterator
Iterator<String> it = sites.iterator();
// Output all elements in the collection
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
Executing the above code produces the following output:
Google Example Taobao Zhihu
Removing Elements
To remove elements from a collection, you can use the remove() method.
In the following example, we remove elements less than 10 from the collection:
Example
import java.util.ArrayList;
import java.util.Iterator;
public class ExampleTest {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(12);
numbers.add(8);
numbers.add(2);
numbers.add(23);
Iterator<Integer> it = numbers.iterator();
while(it.hasNext()) {
Integer i = it.next();
if(i < 10) {
it.remove(); // Remove elements less than 10
}
}
System.out.println(numbers);
}
}
Executing the above code produces the following output:
[12, 23]
Note:The Java iterator is a one-way traversal mechanism, meaning it can only traverse elements in a collection from front to back and cannot traverse backward. Additionally, when using an iterator to traverse a collection, you cannot directly modify elements in the collection; instead, you need to use the iterator's remove() method to delete the current element.
Other Extensions
Java Collections Framework