Monday, 2 January 2017

Java Difference : Iterator vs Enumeration

Iterator and Enumeration differ based on the following parameters:

1.      Introduced
Enumeration was introduced as part of JDK1.0
Iterator was introduced from JDK1.2

2.      Modifying Collection
Enumeration cannot be used to modify the elements in Collection while traversing.
Iterator can be used to remove the element in Collection while traversing (it can remove element using remove() )

3.      Available methods
Enumeration:  hasMoreElements(), nextElement()
Iterator:  hasNext(), next(), remove()

4.      Fail fast or Fail safe
Enumeration is fail safe by nature.
Iterator is fail fast by nature.

Note : Iterator being fail fast by nature is considered safe than enumeration as it throws ConcurrentModificationException, if any other thread tries to modify the Collection that is being traversed by Iterator.

5.      Legacy concept
Enumeration is generally preferred for traversing over legacy classes like Vector, HashTable, etc.
Iterator is used to traverse over almost all classes in java Collection framework like ArrayList, LinkedList, HashMap, HashSet, etc.

Note: Iterator is preferred over Enumeration. 



Example: 

package Traversal;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class TraversalMethods {

      public static void main(String[] args) {
            List<String> arrayList = new ArrayList<String>();
            arrayList.add("Rajneesh");
            arrayList.add("Shweta");
            arrayList.add("Anup");

            Iterator<String> listIterator = arrayList.iterator();
            System.out.println("Traversing list using iterator");

            while(listIterator.hasNext()){

                  String token = (String)listIterator.next();
                  System.out.println(token);
                  if(token.equals("Shweta")){
                        listIterator.remove();   //iterator remove() method facility
                  }
            }
            System.out.println("ArrayList after removal operation");
            System.out.println(arrayList);

            Vector<String> vector = new Vector<String>();
            vector.add("Puja");
            vector.add("Mukesh");
            vector.add("Titli");

            System.out.println("Traversing vector using Enumeration");
            Enumeration<String> enumeration = vector.elements();

            while(enumeration.hasMoreElements()){

                  System.out.println(enumeration.nextElement());
                  //enumeration.remove(); METHOD DOESNOT EXIST
            }

      }

}


In the given example you can see iterator provides you with remove() method whereas in enumeration no such method exists.

Hope this helps. IF there is any query then put in the comment box and we can discuss it and clarify.

You may also like to read:

1 comment:

  1. really a nice read...
    Thanks for sharing

    ReplyDelete

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.