Home » Java programming language

Differences between Enumeration and Iterator in Java

Java Enumeration vs Iterator: Here, we are going to learn what are the differences between Enumeration and Iterator in java? Compare Enumeration v/s Iterator in java? How Enumeration differs from Iterator in java?
Submitted by Preeti Jain, on July 28, 2019

Enumeration vs Iterator in Java

Here, we will see how Enumeration differs from Iterator?

1) Enumeration

  • Enumeration is an interface which is introduced in java.
  • While iterating an element by Enumeration we can traverse only legacy elements so here we will see the concept of legacy
    Legacy: Previous or Earlier versions of java define various classes and one interface to store objects and collection framework didn't include at the time so when collection framework came so these classes are re-engineered to support collection framework.
  • We can create an Enumeration object by using elements() method of Enumeration interface.

    Syntax:

    Enumeration en = collection_object.elements();
  • While iterating an object or an element by Enumeration then we can perform only read operation.
  • Enumeration is faster than Iterator.
  • In the case of Enumeration, we will use two methods to check the existing element and iterating the next element.
    1. boolean hasMoreElements()
    2. Object nextElement()
  • Enumeration concept is applicable only for legacy classes so it does not support few collections that's why it is not a universal interface.
  • By using Enumeration we can get only ReadAccess and we can't perform any remove operation.

Example:

import java.util.*;

class EnumerationClass {
    public static void main(String[] args) {
        // creating a Vector instance
        Vector v = new Vector();

        // add few elements by using addElement() of Vector class
        v.addElement(10);
        v.addElement(20);
        v.addElement(30);
        v.addElement(40);
        v.addElement(50);

        // print the vector list
        System.out.println(v);

        // creating an object of Enumeration 
        Enumeration en = v.elements();

        // loop to check existing more elements
        while (en.hasMoreElements()) {
            Integer in = (Integer) en.nextElement();
        }
        System.out.println(v);
    }
}

Output

E:\Programs>javac EnumerationClass.java
Note: EnumerationClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

E:\Programs>java EnumerationClass
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 50] 

2) Iterator

  • Iterator is an interface which is introduced in java.
  • While iterating an element by Iterator we can perform read and remove operation and we can't perform any other operation like add an object, replace an object.
  • We can create an Iterator object by using iterator() method of Iterator interface.
    Syntax:
    Interface_name object_name = Collection_class.Iterator_method
  • Iterator is slower than Enumeration.
  • In the case of Iterator, we will use two methods to check the existing element and iterating the next element.
    1. boolean hasNext()
    2. Object next()
  • Iterator concept is not applicable only for legacy classes but also support for non-legacy classes so that's why it is a universal interface.

Example:

import java.util.*; 

class IteratorClass {
    public static void main(String[] args) {
        // Creating a Set interface object
        Set set = new HashSet();

        // By using add() method to add few elements in set
        set.add("Java");
        set.add("Python");
        set.add("C++");
        set.add("Ruby");
        set.add("C");

        // Creating an object of Iterator interface
        Iterator itr = set.iterator();

        // loop for traversing an element
        while (itr.hasNext()) {
            String str = (String) itr.next();
            if (str.equals("C"))
                itr.remove();
        }
        // Display elements of Set interface 
        System.out.println("The final list of Iterator :" + set);
    }
}

Output

E:\Programs>javac IteratorClass.java
Note: IteratorClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

E:\Programs>java IteratorClass
The final list of Iterator :[Ruby, Python, C++, Java]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.