Wednesday, June 10, 2026
HomeLanguagesJavaAbstractList iterator() method in Java with Examples

AbstractList iterator() method in Java with Examples

The iterator() method of java.util.AbstractList class is used to return an iterator over the elements in this list in proper sequence.

This implementation returns a straightforward implementation of the iterator interface, relying on the backing list’s size(), get(int), and remove(int) methods.

Syntax:

public Iterator iterator()

Returns Value: This method returns an iterator over the elements in this list in proper sequence.

Below are the examples to illustrate the iterator() method.

Example 1:




// Java program to demonstrate
// iterator() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList<Integer>
            AbstractList<Integer>
                arrlist1 = new ArrayList<Integer>();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print arrlist1
            System.out.println("ArrayList : "
                               + arrlist1);
  
            // creating object of Iterator
            // using iterator() method
            Iterator it = arrlist1.iterator();
  
            // printing the iterated value
            while (it.hasNext()) {
                System.out.println("Value is : "
                                   + it.next());
            }
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

ArrayList : [10, 20, 30, 40, 50]
Value is : 10
Value is : 20
Value is : 30
Value is : 40
Value is : 50

Example 2:




// Java program to demonstrate
// iterator() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList<String>
            AbstractList<String>
                arrlist1 = new ArrayList<String>();
  
            // Populating arrlist1
            arrlist1.add("A");
            arrlist1.add("B");
            arrlist1.add("C");
            arrlist1.add("D");
            arrlist1.add("E");
  
            // print arrlist1
            System.out.println("ArrayList : "
                               + arrlist1);
  
            // creating object of Iterator
            // using iterator() method
            Iterator it = arrlist1.iterator();
  
            // printing the iterated value
            while (it.hasNext()) {
                System.out.println("Value is : "
                                   + it.next());
            }
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

ArrayList : [A, B, C, D, E]
Value is : A
Value is : B
Value is : C
Value is : D
Value is : E
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS