What is the difference between Enumeration and Iterator in Java?

Published May 09, 2022

Java.util provides two interfaces for traversing over the elements of a Collection object: Enumeration and Iterator. Despite the fact that they both traverse a Collection object, there are some differences between them. This post will look at the differences between Enumeration and Iteration in Java.

The Differences between Enumeration and iterator

  • Addition to JDK: The two were introduced at two different times. Enumeration was introduced in JDK 1.0, whereas Iterator was introduced in JDK 1.2

  • Remove Elements: This is the main difference between the two. In the Iterator interface, we can remove an element while traversing the Collection object, whereas we can't alter it when traversing the Collection object using Enumeration. This is because the Iterator interface has a remove() method, and the Enumeration interface does not have. 

  • Type of Operation: Iterator is a fail-fast type of operation, whereas enumeration is a fail-safe. As a result, the iterator will throw ConcurrentModificationException when the collection is modified while iterating except through its own remove() method, while Enumeration will not throw any exceptions when the collection is modified while iterating.

 

Examples of Enumeration and Iterator in Java

 Example of Enumeration

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Enumeration;

import java.util.List;

import java.util.Vector;

public class Enumeration_Example {

      public static void main(String[] args) {

                  List laptoplist = new ArrayList(Arrays.asList( new String[] {"Sumsung", "Lenovo", "Apple", "HP"}));

            Vector vectali = new Vector(laptoplist);

            delete(vectali, "Sumsung");

        }

        private static void delete(Vector vectali, String laptop) {

            Enumeration lapi = vectali.elements();

            while (lapi.hasMoreElements()) {

              String s = (String) lapi.nextElement();

              if (s.equals(laptop)) {

                  vectali.remove(laptop);

              }

            }

            System.out.println("The Laptop brands includes:");

            lapi = vectali.elements();

            while (lapi.hasMoreElements()) {

              System.out.println(lapi.nextElement());

            }

      }

}

 

Output

The Laptop brands includes:

Lenovo

Apple

HP

 

Example of Iterator

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Iterator;

import java.util.List;

import java.util.Vector;

public class Iterator_example {

      public static void main(String[] args) {

                  List laptoplist = new ArrayList(Arrays.asList( new String[] {"Sumsang", "Lenovo", "HP", "Apple"}));

            Vector vectali = new Vector(laptoplist);

            delete(vectali, "HP");

        }

        private static void delete(Vector vectali, String name) {

            Iterator a = vectali.iterator();

            while (a.hasNext()) {

              String s = (String) a.next();

              if (s.equals(name)) {

                  a.remove();

              }

            }

            // Display the names

            System.out.println("The laptop brand includes:");

            a = vectali.iterator();

            while (a.hasNext()) {

              System.out.println(a.next());

            }

      }

}

 

Output

The laptop brand includes:

Sumsang

Lenovo

Apple

 

Conclusion

In conclusion, whenever you need a queue, use a PriorityQueue, and when you need a set, use a TreeSet. A TreeSet has unique elements but does not offer the API of a Queue. A Queue does not offer the API of a Set and can have multiple elements of the same type

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

116 Views