Java Iterator

A Java Iterator is not a collection. It is a method for accessing collections that can be used to iterate over collections like ArrayList and HashSet.

Iterator is the simplest implementation of a Java Iterator. ListIterator is an interface in the Collection API that extends the Iterator interface.

The two basic operations on iterator IT are next, hasNext, and remove.

Calling it.next() returns the next element of the iterator and updates the iterator’s state.

Call it.hasNext() to check if there are any more elements in the collection.

Call it.remove() to remove the element returned by the iterator.

The Iterator class is in the java.util package and needs to be imported before use. The syntax is as follows:

import java.util.Iterator; // Introduce the Iterator class
Copy the code

Get an iterator

To get an iterator, use the iterator() method:

The instance

// Introduce the ArrayList and Iterator classes
import java.util.ArrayList;
import java.util.Iterator;

public class RunoobTest {
    public static void main(String[] args) {

        // Create a collection
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");

        // Get the iterator
        Iterator<String> it = sites.iterator();

        // Outputs the first element in the collectionSystem.out.println(it.next()); }}Copy the code

After executing the above code, the output is as follows:

Google
Copy the code

Loop set element

The simplest way to tell iterator it to return all the elements of the collection one by one is to use the while loop:

while(it.hasNext()) {
    System.out.println(it.next());
}
Copy the code

The following outputs all elements in the collection Sites:

The instance

// Introduce the ArrayList and Iterator classes
import java.util.ArrayList;
import java.util.Iterator;

public class RunoobTest {
    public static void main(String[] args) {

        // Create a collection
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");

        // Get the iterator
        Iterator<String> it = sites.iterator();

        // Prints all the elements in the collection
        while(it.hasNext()) { System.out.println(it.next()); }}}Copy the code

After executing the above code, the output is as follows:

Google
Runoob
Taobao
Zhihu
Copy the code

Remove elements

To remove elements from a collection, use the remove() method.

In the following example, we remove elements less than 10 from the collection:

The instance

// Introduce the ArrayList and Iterator classes
import java.util.ArrayList;
import java.util.Iterator;

public class RunoobTest {
    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); }}Copy the code

After executing the above code, the output is as follows:

[12, 23] it.remove(); // Delete elements less than 10}} system.out.println (numbers); }}Copy the code

After executing the above code, the output is as follows:

[23] 12,Copy the code