Easy Tutorial
❮ Java String Replacefirst Net Urldate ❯

Java Iterator

Java Collections Framework

Java Iterator is not a collection; it is a method used to access collections, such as ArrayList and HashSet.

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

The two basic operations of an iterator it are next, hasNext, and remove.

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

Calling it.hasNext() checks if there are more elements in the collection.

Calling it.remove() removes the element returned by the iterator.

The Iterator class is located in the java.util package, and you need to import it before use, with the following syntax:

import java.util.Iterator; // Import the Iterator class

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 tutorialproTest {
    public static void main(String[] args) {

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

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

        // Print the first element of the collection
        System.out.println(it.next());
    }
}

Executing the above code will produce the following output:

Google

Looping Through Collection Elements

The simplest way to iterate through all elements of a collection using an iterator it is by using a while loop:

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

The following example prints all elements in the sites collection:

Example

// Import ArrayList and Iterator classes
import java.util.ArrayList;
import java.util.Iterator;

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

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

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

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

Executing the above code will produce the following output:

Google
tutorialpro
Taobao
Zhihu

Removing Elements

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

The following example removes elements less than 10 from the collection:

Example

// Import ArrayList and Iterator classes
import java.util.ArrayList;
import java.util.Iterator;

public class tutorialproTest {
    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();
            }
        }
        System.out.println(numbers);
    }
}

Executing the above code will produce the following output:

[12, 23]
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 will produce the following output:

[12, 23]

Java Collections Framework

❮ Java String Replacefirst Net Urldate ❯