Skip to content
Related Articles

Related Articles

Java AbstractSequentialList | iterator() method
  • Last Updated : 26 Nov, 2018

AbstractSequentialList iterator() method in Java is used to get an Iterator or List Iterator over a list. It returns an iterator over the elements in this list (in proper sequence). This method overrides iterator() in class AbstractList<E>

Syntax:

public Iterator<E> iterator()

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

Below is the code to illustrate iterator() method:

Program 1:






// Java program to demonstrate
// iterator() method
  
import java.util.*;
  
public class GfG {
  
    public static void main(String[] args)
    {
        // Creating an instance of the AbstractSequentialList
        AbstractSequentialList<Integer> absl = new LinkedList<>();
  
        // adding elements to absl
        absl.add(5);
        absl.add(6);
        absl.add(7);
        absl.add(2, 8);
        absl.add(2, 7);
        absl.add(1, 9);
        absl.add(4, 10);
  
        // Getting Iterator
        Iterator<Integer> Itr = absl.iterator();
  
        // Traversing elements with help of iterator
        while (Itr.hasNext()) {
            System.out.print(Itr.next() + " ");
        }
    }
}


Output:

5 9 6 7 10 8 7

Program 2:




// Java program to demonstrate
// iterator() method
  
import java.util.*;
  
public class GfG {
  
    public static void main(String[] args)
    {
        // Creating an instance of the AbstractSequentialList
        AbstractSequentialList<String> absl = new LinkedList<>();
  
        // adding elements to absl
        absl.add("Geeks");
        absl.add("ForGeeks");
        absl.add("A");
        absl.add("Portal");
        absl.add(3, "Computer Science");
        absl.add(5, "For Geeks");
  
        // Getting Iterator
        Iterator<String> Itr = absl.iterator();
  
        // Traversing elements with help of iterator
        while (Itr.hasNext()) {
            System.out.println(Itr.next());
        }
    }
}


Output:

Geeks
ForGeeks
A
Computer Science
Portal
For Geeks

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :