Skip to content
Related Articles

Related Articles

Improve Article

Iterators in Java

  • Difficulty Level : Medium
  • Last Updated : 20 Jul, 2021

Iterators are used in the Collection framework in Java to retrieve elements one by one. There are three iterators in Java

  1. Enumeration
  2. Iterator
  3. ListIterator

1. Enumeration

It is an interface used to get elements of legacy collections(Vector, Hashtable). Enumeration is the first iterator present from JDK 1.0, rests are included in JDK 1.2 with more functionality. Enumerations are also used to specify the input streams to a SequenceInputStream. We can create an Enumeration object by calling elements() method of vector class on any vector object 
 

// Here "v" is an Vector class object. e is of
// type Enumeration interface and refers to "v"
Enumeration e = v.elements();

There are two methods in the Enumeration interface namely : 

// Tests if this enumeration contains more elements
public boolean hasMoreElements();

// Returns the next element of this enumeration 
// It throws NoSuchElementException
// if no more element present
public Object nextElement();

Java




// Java program to demonstrate Enumeration
 
// Importing Enumeration and Vector classes
// from java.util package
import java.util.Enumeration;
import java.util.Vector;
 
// Main class
public class Test
{
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a vector object
        Vector v = new Vector();
       
      // Iterating over vector object
      for (int i = 0; i < 10; i++)
            v.addElement(i);
       
      // Printing elements in vector object 
      System.out.println(v);
 
        // At beginning e(cursor) will point to
        // index just before the first element in v
        Enumeration e = v.elements();
 
        // Checking the next element availability where
        // condition holds true till threre is a single element
      // remaining in the List
        while (e.hasMoreElements())
        {
            // Moving cursor to next element
            int i = (Integer)e.nextElement();
 
            // Print above elements in object
            System.out.print(i + " ");
        }
    }
}

Output: 



[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0 1 2 3 4 5 6 7 8 9 

There are certain limitations of enumeration which are as follows: 

  • Enumeration is for legacy classes(Vector, Hashtable) only. Hence it is not a universal iterator.
  • Remove operations can’t be performed using Enumeration.
  • Only forward direction iterating is possible.

2. Iterator

It is a universal iterator as we can apply it to any Collection object. By using Iterator, we can perform both read and remove operations. It is an improved version of Enumeration with the additional functionality of remove-ability of an element.

Iterator must be used whenever we want to enumerate elements in all Collection framework implemented interfaces like Set, List, Queue, Deque, and also in all implemented classes of Map interface. Iterator is the only cursor available for the entire collection framework.

Iterator object can be created by calling iterator() method present in Collection interface.

Syntax:

Iterator itr = c.iterator();

Note: Here “c” is any Collection object. itr is of type Iterator interface and refers to ā€œcā€.

Iterator interface defines three methods as listed below:

1. Returns true if the iteration has more elements



public boolean hasNext();

2. Returns the next element in the iteration. It throws NoSuchElementException if no more element present

public Object next();

3. Remove the next element in the iteration. This method can be called only once per call to next()

public void remove();

Note: remove() method can throw two exceptions namely as follows:

  • UnsupportedOperationException : If the remove operation is not supported by this iterator
  • IllegalStateException : If the next method has not yet been called, or the remove method has already been called after the last call to the next method.

 Example:

Java




// Java program to Demonstrate Iterator
 
// Importing ArrayList and Iterator classes
// from java.util package
import java.util.ArrayList;
import java.util.Iterator;
 
// Main class
public class Test {
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an ArrayList class object
        // Declaring object of integer type
        ArrayList<Integer> al = new ArrayList<Integer>();
 
        // Iterating over the List
        for (int i = 0; i < 10; i++)
            al.add(i);
 
        // Printing the elements in the List
        System.out.println(al);
 
        // At the beginning itr(cursor) will point to
        // index just before the first element in al
        Iterator itr = al.iterator();
 
        // Checking the next element  where
        // condition holds true till there is single element
        // in the List using hasnext() method
        while (itr.hasNext()) {
            //  Moving cursor to next element
            int i = (Integer)itr.next();
 
            // Getting even elements one by one
            System.out.print(i + " ");
 
            // Removing odd elements
            if (i % 2 != 0)
                itr.remove();
        }
 
        // Command for next line
        System.out.println();
 
        // Printing the elements inside the object
        System.out.println(al);
    }
}
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0 1 2 3 4 5 6 7 8 9 
[0, 2, 4, 6, 8]

Also, there are certain limitations of Iterator which are listed as follows: 

  • Only forward direction iterating is possible.
  • Replacement and addition of new element is not supported by Iterator.

3. ListIterator

It is only applicable for List collection implemented classes like ArrayList, LinkedList, etc. It provides bi-directional iteration. ListIterator must be used when we want to enumerate elements of List. This cursor has more functionality(methods) than iterator. ListIterator object can be created by calling listIterator() method present in the List interface.
 

ListIterator ltr = l.listIterator();

Note: Here ā€œlā€ is any List object, ltr is of type. ListIterator interface and refers to “l”. ListIterator interface extends the Iterator interface. So all three methods of Iterator interface are available for ListIterator. In addition, there are six more methods. 

1. Forward direction

1.1 Returns true if the iteration has more elements



public boolean hasNext();

1.2 Same as next() method of Iterator

public Object next();

1.3 Returns the next element index or list size if the list iterator is at the end of the list.

public int nextIndex();

2. Backward direction

2.1 Returns true if the iteration has more elements while traversing backward.

public boolean hasPrevious();

2.2 Returns the previous element in the iteration and can throw NoSuchElementException if no more element present.

public Object previous();

2.3 Returns the previous element index or -1 if the list iterator is at the beginning of the list

public int previousIndex();

3. Other Methods

3.1 Same as remove() method of Iterator

public void remove();

3.2 Replaces the last element returned by next() or previous() with the specified element.

public void set(Object obj); 

3.3 Inserts the specified element into the list at the position before the element that would be returned by next()



public void add(Object obj);

Clearly, the three methods that ListIterator inherits from Iterator (hasNext(), next(), and remove()) do exactly the same thing in both interfaces. The hasPrevious() and the previous operations are exact analogues of hasNext() and next(). The former operations refer to the element before the (implicit) cursor, whereas the latter refers to the element after the cursor. The previous operation moves the cursor backward, whereas the next moves it forward.

ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().

1. set() method can throw 4 exceptions. 

  • UnsupportedOperationException: if the set operation is not supported by this list iterator
  • ClassCastException: If the class of the specified element prevents it from being added to this list
  • IllegalArgumentException: If some aspect of the specified element prevents it from being added to this list
  • IllegalStateException: If neither next nor previous have been called, or remove or add have been called after the last call to next or previous

2. add() method can throw 3 exceptions. 

  • UnsupportedOperationException: If the add method is not supported by this list iterator
  • ClassCastException: If the class of the specified element prevents it from being added to this list
  • IllegalArgumentException: If some aspect of this element prevents it from being added to this list

Example:

Java




// Java program to demonstrate ListIterator
 
// Importing ArrayList and List iterator classes
// from java.util package
import java.util.ArrayList;
import java.util.ListIterator;
 
// Main class
public class Test {
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of ArrayList class
        ArrayList al = new ArrayList();
 
        // Iterating over Arraylist object
        for (int i = 0; i < 10; i++)
 
            // Adding elements to the Arraylist object
            al.add(i);
 
        // Print and display all elements inside object
        // created above
        System.out.println(al);
 
        // At beginning ltr(cursor) will point to
        // index just before the first element in al
        ListIterator ltr = al.listIterator();
 
        // Checking the next element availability
        while (ltr.hasNext()) {
            //  Moving cursor to next element
            int i = (Integer)ltr.next();
 
            // Getting even elements one by one
            System.out.print(i + " ");
 
            // Changing even numbers to odd and
            // adding modified number again in
            // iterator
            if (i % 2 == 0) {
                // Change to odd
                i++;
                // Set method to change value
                ltr.set(i);
                // To add
                ltr.add(i);
            }
        }
 
        // Print and dispplay statements
        System.out.println();
        System.out.println(al);
    }
}

Output: 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0 1 2 3 4 5 6 7 8 9 
[1, 1, 1, 3, 3, 3, 5, 5, 5, 7, 7, 7, 9, 9, 9]

Note: Similarly, there are certain limitations with ListIterator. It is the most powerful iterator but it is only applicable for List implemented classes, so it is not a universal iterator.

Important Points

  1. Please note that initially, any iterator reference will point to the index just before the index of the first element in a collection.
  2. We don’t create objects of Enumeration, Iterator, ListIterator because they are interfaces. We use methods like elements(), iterator(), listIterator() to create objects. These methods have an anonymous Inner Class that extends respective interfaces and return this class object. This can be verified by the below code. For more on inner class refer 
     

Java




// Java program to demonstrate iterators references
 
// Importing required classes from java.util package
import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;
 
// Main class
public class GFG {
   
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of Vector class
        Vector v = new Vector();
 
        // Creting three iterators
        Enumeration e = v.elements();
        Iterator itr = v.iterator();
        ListIterator ltr = v.listIterator();
 
        // Print class names of iterators
        // using getClass() and getName() methods
        System.out.println(e.getClass().getName());
        System.out.println(itr.getClass().getName());
        System.out.println(ltr.getClass().getName());
    }
}
Output
java.util.Vector$1
java.util.Vector$Itr
java.util.Vector$ListItr

Note: The $ symbol in reference class name is a proof that concept of inner classes is used and these class objects are created. 

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. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :