When to use Enumeration and when to use Iterator

Anyone tell me when to use Enumeration and when to use Iterator?

Use Iterator always.
One big difference between these two is (apart from what Pallav has already mentioned), iterator is fail-safe. If you are using an iterator to go through a collection you can be sure of no concurrent modifications in the underlying collection which may
happen in multi-threaded environments.

Well, apart from being an advantage this also is said to decrease performance by a tad. However, that's negligible. (Some amount of processing is involved in checking for modifications). If you see the docs it says about "well defined semantics" for the iterator, the fail-safe property adds to that.

-- Vinay Binny

Enumeration and Iterator are interfaces in java.util package.
Enumeration is the old Interface(for legacy classes like Hashtable).
Iterator is for the new classes like HashSet,HashMap..
Enumeration and Iterator are used to just count the no of elements and view it.
But Iterator has an additional method for deleting elements.
So when you intend to delete elements. you can go for the Iterator Interface.

-- Bava

All of the above are correct but we have forgotten about one thing - iterator is a design pattern and the purpose is to abstract looping.  For example you have a class that loops thru Lists or ArraysLists so you used Enumeration.  But suddenly another programmer made an Array (say String[]) that can only do for loop.  Your enumeration is screwed I would say.  So the strategy is to use Iterator.   Now first you will have this method in your class:

//this is Loop.java
public void loopThis(Iterator iter){
   while(iter.hasNext()){
      Object obj = iter.next();
   }
}

So you use it this way:
Loop loop = new Loop();
ArrayList daList = new ArrayList();
loop.loopThis(daList.keys().iterate());

Now this part is to show you haw to make an iterator out of a String[]

1. Make an Iterator Class

public class CustomIterator implements java.util.Iterator{

    String[] arry;
    int rowPosition = 0;
    int lngth = 0;

    public CustomIterator(String[] arry){
        this.arry = arry;
        this.lngth = arry.length;
    }

    public boolean hasNext(){
        if(rowPosition >= lngth) return false;
        else return true;
    }

    public Object next(){
        Object obj = arry[rowPosition];
        rowPosition++;
        return obj;
    }

    public void remove(){
        for(int i = rowPosition-1; i < (lngth -1); i++){
            arry[i] = arry[i+1]; // move up data
        }
        arry[lngth-1] = null;
        lngth--;
        rowPosition--;
     }
}

and here is how you do it:

Loop loop = new Loop();
ArrayList daList = new ArrayList();
loop.loopThis(daList.keys().iterate());

String[] str = new String[]{"1","2","3","4","5"};
CustomIterator iter = new CustomIterator(str);
loop.loopThis(iter);

-- Steve Torrefranca

Well, not really. What if I implement the iterator design pattern using the enumeration interface? The Iterator design pattern does'nt make it mandatory to implement a remove method. All it says is to give a iteration capability irrespective of the underlying collection. I'll make Custom Iterator implement Enumeration without the remove method. Now we've implemented the iterator design pattern using enumeration and the enumeration works fine. The eponymous iterator interface may have derived its name from the iterator pattern but does'nt mean that an Enumeration can use the iterator design pattern.

-- Vinay Binny

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.