Open In App

Enumeration vs Iterator vs ListIterator in Java

Last Updated : 02 Sep, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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


Output

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

Property               Enumeration                  Iterator              ListIterator
1. Where we can apply? It can be applied only to the legacy classes. It can be applied to any collection interface. It can be applied to the only list interface.
2. Is it a legacy?   Yes (introduced in 1.0 V).  No (introduced in 1.2 V). No (introduced in 1.2 V).
3. Allowed Movement Single direction, i.e we can traverse elements present in the collection only in the forward direction. Single direction, i.e we can traverse elements present in the collection only in the forward direction. Bidirectional, i.e we can traverse elements present in the collection both in forward and backward directions.
4. Allowed  Operation We can only perform the read operation. We can perform read and remove operation. We can perform read, remove, add, and replace operations.
5. How can we get it? By calling elements() method present in the vector class. By calling iterator() method present in any collection interface. By calling listIterator() method present in the list interface.


Similar Reads

Difference between an Iterator and ListIterator in Java
Iterators are used in Collection framework in Java to retrieve elements one by one. It can be applied to any Collection object. By using Iterator, we can perform both read and remove operations. Iterator must be used whenever we want to enumerate elements in all Collection framework implemented interfaces like Set, List, Queue, Deque and also in al
3 min read
Retrieving Elements from Collection in Java (For-each, Iterator, ListIterator &amp; EnumerationIterator)
Prerequisite: Collection in Java Following are the 4 ways to retrieve any elements from a collection object: For-each For each loop is meant for traversing items in a collection. // Iterating over collection 'c' using for-each for (Element e: c) System.out.println(e); We read the ‘:’ used in for-each loop as “in”. So loop reads as “for each element
3 min read
Difference between Iterator and Enumeration in Java with Examples
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 impl
2 min read
LinkedList listIterator() Method in Java
The Java.util.LinkedList.listIterator() method is used to return a list-iterator containing the same elements as that of the LinkedList in proper and same sequence starting from a specific position or index number which is passed as a parameter to this method. Syntax: ListIterator new_list = LinkedList.listIterator(int index); Parameters: The param
1 min read
Reverse an ArrayList in Java using ListIterator
Assuming you have gone through arraylist in java and know about arraylist. This post contains different examples for reversing an arraylist which are given below:1. By writing our own function(Using additional space): reverseArrayList() method in RevArrayList class contains logic for reversing an arraylist with integer objects. This method takes an
6 min read
Java AbstractSequentialList | ListIterator()
AbstractSequentialList listIterator(): method in Java is used to get a listIterator over this list. It returns a list iterator over the elements in this list (in proper sequence). Syntax: public abstract ListIterator listIterator(int index) Parameters: This method takes a parameter index which is the index of first element to be returned from the l
2 min read
CopyOnWriteArrayList listIterator() method in Java
The listIterator() method of CopyOnWriteArrayList returns an listIterator over the elements in this list in proper sequence. The listIterator does NOT support the remove, set or add methods. Syntax: public ListIterator listIterator() Parameters: The function does not accept any parameters. Return Value: The function returns a list iterator over the
3 min read
ArrayList listIterator() method in Java with Examples
listIterator() The listIterator() method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in proper sequence). The returned list iterator is fail-fast. Syntax: public ListIterator listIterator() Return Value: This method returns a list iterator over the elements in this list (in proper sequence). Below
3 min read
AbstractList listIterator() Method in Java with Examples
The listIterator() method of java.util.AbstractList class is used to return a list-iterator containing the same elements as that of the AbstractList in proper and same sequence starting from a specific position or index number which is passed as a parameter to this method. Syntax: ListIterator new_list = AbstractList.listIterator(int index); Parame
2 min read
Stack listIterator(int) method in Java with Example
The listIterator(int) method of Stack Class is used to return a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified
2 min read