Java Iterator
[ Java Collection Framework](#)
Java Iterator is a mechanism in the Java Collection Framework, which is an interface used to traverse collections (such as lists, sets, maps, etc.).
It provides a unified way to access elements in a collection without needing to know the specific implementation details of the underlying collection.
Java Iterator is not a collection, but a method for accessing collections. It can be used to iterate over collections like (#) and (#).
Iterator is the simplest implementation of Java Iterator. ListIterator is an interface in the Collection API, which extends the Iterator interface.
!(#)
The Iterator interface defines several methods, the most commonly used are the following three:
* **next()** - Returns the next element in the iterator and moves the iterator's pointer to the next position.
* **hasNext()** - Used to check if there are more elements to access in the collection.
* **remove()** - Removes the last element returned by the iterator from the collection (optional operation).
The Iterator class is located in the java.util package and needs to be imported before use. The syntax is as follows:
import java.util.Iterator; // Import the Iterator class
By using iterators, 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 works with various types of collections.
### Getting an Iterator
To get an iterator from a collection, you can use the iterator() method:
## Example
// Import ArrayList and Iterator classes
import java.util.ArrayList;
import java.util.Iterator;
public class TutorialTest {
public static void main(String[] args){
// Create a collection
ArrayList sites =new ArrayList();
sites.add("Google");
sites.add("Tutorial");
sites.add("Taobao");
sites.add("Zhihu");
// Get an iterator
Iterator it = sites.iterator();
// Output the first element in the collection
System.out.println(it.next());
}
}
Execute the above code, the output is as follows:
Google
When using an iterator to traverse a collection, if the collection is modified during traversal (such as adding or removing elements), it may cause a ConcurrentModificationException. To avoid this problem, you can use the iterator's own **remove()** method to perform deletion operations.
### Looping Through Collection Elements
The simplest way to have 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 ArrayList and Iterator classes
import java.util.ArrayList;
import java.util.Iterator;
public class TutorialTest {
public static void main(String[] args){
// Create a collection
ArrayList sites =new ArrayList();
sites.add("Google");
sites.add("Tutorial");
sites.add("Taobao");
sites.add("Zhihu");
// Get an iterator
Iterator it = sites.iterator();
// Output all elements in the collection
while(it.hasNext()){
System.out.println(it.next());
}
}
}
Execute the above code, the output is as follows:
GoogleTutorialTaobaoZhihu
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 ArrayList and Iterator classes
import java.util.ArrayList;
import java.util.Iterator;
public class TutorialTest {
public static void main(String[] args){
ArrayList numbers =new ArrayList();
numbers.add(12);
numbers.add(8);
numbers.add(2);
numbers.add(23);
Iterator it = numbers.iterator();
while(it.hasNext()){
Integer i = it.next();
if(i <10){
it.remove();// Remove elements less than 10
}
}
System.out.println(numbers);
}
}
Execute the above code, the output is as follows:
[12, 23]
**Note:** Java Iterator is a unidirectional traversal mechanism, meaning it can only traverse elements in the collection from front to back and cannot traverse backwards. Also, when traversing a collection using an iterator, you cannot directly modify elements in the collection. Instead, you need to use the iterator's remove() method to delete the current element.
[ Java Collection Framework](#)
YouTip