Java AbstractSequentialList | iterator() method

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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() + " ");
        }
    }
}

chevron_right


Output:

5 9 6 7 10 8 7

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}

chevron_right


Output:

Geeks
ForGeeks
A
Computer Science
Portal
For Geeks

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.