Java – Iterator Interface


  • It is an interface.
  • Iterator providing methods to iterator any collection.
  • iterator() method returns Iterator object of any collection.


  1. boolean hasNext(): checks the next element is present or not to iterate.
  2. Object next(): returns the next element of iterator object.

Program to display ArrayList using Iterator:

import java.util.*;
class Code
            public static void main(String[] args)
                        List<Integer> list = new ArrayList<Integer>();
                        for(int i=1 ; i<=5 ; i++)
                        System.out.println(“Display using Iterator :”);
                        Iterator<Integer> itr = list.iterator();
                                    Integer ele =;

When we use for/for-each/iterator?

For-loopFor-each loopIterator
Index based.Not index based.Not index based.
Process only List(index based)Process List, Set and MapProcess List, Set and Map
Use get(index) methodDo not use any other methodDo not use any other method
Scroll to Top