AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-collection: What is the difference between enumerator and iterator method?

Please explain with example the difference between enumerator and iterator methods.

java x 211
collection x 52
Posted On : 2015-08-04 00:14:58.0
profile Garima Gupta - anyforum.in Garima Gupta
596129544529
up-rate
4
down-rate

Answers


Iterator:
-----------------------------------------
Iterator is an interface of java.util package.

It has three methods:
------------------------------------------
*hasNext()
*next()
*remove()

Enumeration:
-----------------------------------------
Enumeration is also an interface of java.util package. An enumeration is an object that generates elements one at a time. It is used for passing through a collection, usually of unknown size. The traversing of elements can only be done once per creation.

It has following methods:
-------------------------------------------------

*hasMoreElements()
*nextElement()


** An iterator over a collection. Iterator takes the place of Enumeration in the Java collections framework.

Iterators differ from enumerations in two ways:
-------------------------------------------------------------------------------------------
* Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
* Method names improved.

Let´s consider the following example:
------------------------------------------------------------------

Performance.java:
---------------------------------------
import java.util.*;
public class Performance {
public static void main(String[] args){
Vector v=new Vector();
Object element;
Enumeration enum;
Iterator iter;
long start;

for(int i=0; i<1000000; i++){
v.add("New Element");
}

enum=v.elements();
iter=v.iterator();
/*******************CODE BLOCK FOR ITERATOR*******************/
start=System.currentTimeMillis();
while(iter.hasNext()){
element=iter.next();
}
System.out.println("Iterator took " + (System.currentTimeMillis()-start));
/*******************END OF ITERATOR BLOCK*******************/

System.gc(); //request to GC to free up some memory
/*******************CODE BLOCK FOR ENUMERATION*******************/
start=System.currentTimeMillis();
while(enum.hasMoreElements()){
element=enum.nextElement();
}
System.out.println("Enumeration took " + (System.currentTimeMillis()-start));
/*******************END OF ENUMERATION BLOCK*******************/
}
}




*Why not use for(int i=0; i< v.size();i++){}?
----------------------------------------------------------------------------------------
For loops are expensive to the processor when the collection reaches large sizes, as many operations are done just to compute the first line:


int i = 0 is an assignment and creation (2 operations)
i get size, check value of i, and compare (3 operations)
i++ gets i then adds 1 to it [++i is only 2 operations] this one (3 operations)
*7/8 operations in total, each time the loop runs through

where an enumeration or iterator uses a while(){}:
--------------------------------------------------------------------------------------
while(v.hasNext()) has next true or false (1 operation)
while(v.hasMoreElements()) has more true or false (1 operation)
*Only one operation per repeat of this loop


Only major difference between Enumeration and iterator is Iterator has a remove() method while Enumeration doesn´t. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as by using Iterator we can manipulate the objects like adding and removing the objects from collection e.g. Arraylist.

Also Iterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException.

In Summary both Enumeration and Iterator will give successive elements, but Iterator is new and improved version where method names are shorter, and has new method called remove.

Posted On : 2015-08-04 01:03:13
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188237098
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in