The iterator() method of ArrayList class in Java Collection Framework is used to get an iterator over the elements in this list in proper sequence. The returned iterator is fail-fast.
Syntax:
Iterator iterator()
Parameter: This method do not accept any parameter.
Return Value: This method returns an iterator over the elements in this list in proper sequence
Below examples illustrate the ArrayList.iterator() method:
Program 1:
// Java code to illustrate iterator() import java.util.*; public class GFG { public static void main(String[] args) { // Create and populate the list ArrayList<String> list = new ArrayList<>(); list.add("Geeks"); list.add("for"); list.add("Geeks"); list.add("is"); list.add("a"); list.add("CS"); list.add("Students"); list.add("Portal"); // Displaying the list System.out.println("The list is: \n" + list); // Create an iterator for the list // using iterator() method Iterator<String> iter = list.iterator(); // Displaying the values after iterating // through the list System.out.println("\nThe iterator values" + " of list are: "); while (iter.hasNext()) { System.out.print(iter.next() + " "); } } } |
The list is: [Geeks, for, Geeks, is, a, CS, Students, Portal] The iterator values of list are: Geeks for Geeks is a CS Students Portal
Program 2:
// Java code to illustrate iterator() import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty ArrayList ArrayList<Integer> list = new ArrayList<Integer>(); // Use add() method to add // elements into the list list.add(10); list.add(15); list.add(30); list.add(20); list.add(5); // Displaying the list System.out.println("The list is: \n" + list); // Create an iterator for the list // using iterator() method Iterator<Integer> iter = list.iterator(); // Displaying the values // after iterating through the list System.out.println("\nThe iterator values" + " of list are: "); while (iter.hasNext()) { System.out.print(iter.next() + " "); } } } |
The list is: [10, 15, 30, 20, 5] The iterator values of list are: 10 15 30 20 5
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- ArrayList of ArrayList in Java
- AbstractList iterator() method in Java with Examples
- AbsractCollection iterator() Method in Java with Examples
- DelayQueue iterator() method in Java with Examples
- Vector iterator() method in Java with Examples
- Set iterator() method in Java with Examples
- Path iterator() method in Java with Examples
- SortedSet iterator() method in Java with Examples
- BlockingDeque iterator() method in Java with examples
- ArrayList toArray() method in Java with Examples
- ArrayList get(index) method in Java with examples
- ArrayList ensureCapacity() method in Java with Examples
- ArrayList removeAll() method in Java with Examples
- ArrayList listIterator() method in Java with Examples
- ArrayList set() method in Java with Examples
- ArrayList size() method in Java with Examples
- ArrayList subList() method in Java with Examples
- ArrayList clone() method in Java with Examples
- Difference between Iterator and Enumeration in Java with Examples
- Java.util.ArrayList.add() Method in Java
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.

