Enumeration vs Iterator vs ListIterator in Java

02 Sep, 2020
Enumeration is an interface. It is used in the collection framework in java to retrieve the elements one by one. Enumeration is a legacy interface that is applicable only for legacy classes like Vector, HashTable, Stack, etc. It provides a single direction iteration. By using enumeration, we can perform only read operation, and we cannot perform remove operation.
Enumeration object can be created by calling elements() method present in the Vector class.
// Here "v" is a vector object. enum is of
// type Enumeration interface and refers to "v"
Enumeration enum = v.elements();

An iterator is a universal cursor that can be applied to any collection object. It provides a single direction iteration. By using an iterator, we can perform both read and remove operation, but we cannot perform replace operation. Iterator must be used whenever we want to enumerate elements in all collection framework implemented interfaces like Set, List, Queue, DeQue, and also implemented classes of Map interface.
Iterator object can be created by calling iterator() method present in the Collection interface
// Here "c" is any collection object. itr is of
// type Iterator interface and refers to "c"
Iterator itr = c.iterator();

ListIterator is the most powerful cursor among all the three cursors. ListIterator is only applicable for list implemented classes like ArrayList, LinkedList, Stack, etc. ListIterator traverses both in the forward and backward direction. By using ListIteartor, we can perform read, remove, and replace operation. The ListIterator must be used when we want to enumerate elements of the list.
ListIterator object can be created by calling listIterator() method present in the list interface.
// Here "l" is any list object. ltr is of
// type ListIterator interface and refers to "l"
ListIterator ltr = l.listIterator();

Java
// A Java program to demonstrates the // difference between Enumeration,// Iterator, and ListIterator  import java.io.*;import java.util.*;  class GFG {          public static void main(String args[])    {                  // Creating a vector object        Vector<Integer> v = new Vector<Integer>();                  // Adding elements to the vector object        v.add(10);        v.add(20);        v.add(30);        v.add(40);        v.add(50);                  System.out.println("Enumeration: ");                  // Creating an Enumeration object        Enumeration e = v.elements();                  // Checking the next element availability        while (e.hasMoreElements()) {                          // Moving cursor to the next element            int i = (Integer)e.nextElement();                          // Printing the element            System.out.print(i + " ");        }        System.out.println();        System.out.println();                  System.out.println("Iterator: ");                  // Creating Iterator object        Iterator<Integer> itr = v.iterator();                  // Checking the next element availability        while (itr.hasNext()) {                          // Moving cursor to the next element            int i = (Integer)itr.next();                          // Checking if i == 10 then            // remove the element            if (i == 10)                itr.remove();        }        System.out.println(v);        System.out.println();                  System.out.println("ListIterator: ");                  // Creating ListIterator object        ListIterator<Integer> ltr = v.listIterator();                  // Checking the next element availability        while (ltr.hasNext()) {                          // Moving cursor to the next element            int i = (Integer)ltr.next();                          // Performing add, remove, and             // replace operation            if (i == 20)                ltr.remove();                          else if (i == 30)                ltr.add(60);                          else if (i == 40)                ltr.set(100);        }                  System.out.println(v);    }}
Enumeration: 
10 20 30 40 50 

Iterator: 
[20, 30, 40, 50]

ListIterator: 
[30, 60, 100, 50]




Table showing the Difference between Enumeration, Iterator, and ListIterator
Java-Enumeration
Java-Iterator
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.