| Author |
??? Difference Between Iterator & Enumeration ???
|
AmitVijay AVKulkarni
Greenhorn
Joined: Apr 26, 2005
Posts: 17
|
|
Dear all, If I have a Vector which has say some values taken from database. Now I want to extract those values, I can use either Enumeration or Iterator. So can anybody tell me what is a difference these two? And which is a best to use? ~ Amit
|
 |
Stuart Gray
Ranch Hand
Joined: Apr 21, 2005
Posts: 410
|
|
In short, Enumeration and Iterator do a similar thing but Iterator is now preferred. The Collection classes such as List implement Iterable and hence use Iterators. From the Enumeration JavaAPI docs:
NOTE: The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.
To be honest, you might also want to consider whether it is really essential to use a Vector, or if you can use something else like an ArrayList.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Additionally, Iterators are fail-fast. If the underlying collection is changed while iterating, Enumerations will produce undefined behaviour, whereas Iterators throw a ConcurrentModificationException. (This probably should go into the FAQ...)
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
|
Note that not all Iterators are fail-fast. This behavior is guaranteed for Iterators of most of the standard implementations: ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap, LinkedHashMap, LinkedHashSet. But it's not guaranteed for Iterators in general. For example most of the classes that might be returned from methods in the Collections class do not have fail-fast behavior.
|
"I'm not back." - Bill Harding, Twister
|
 |
Monoranjan Gorai
Greenhorn
Joined: Jul 25, 2005
Posts: 19
|
|
Hi, I would suggest to use Enumeration insteed of Iterator. Because Enumeration is very fast compared to Iterator. See the example below: /** Created By: Monoranjan Gorai */ 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 ENUMERATION******************* start=System.currentTimeMillis(); while(enum.hasMoreElements()) { element =enum.nextElement(); } System.out.println("Enumeration took " + (System.currentTimeMillis()-start)); //*****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*/ //************END OF ENUMERATION BLOCK********************** } }
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
Monoranjan, please don't post the same question in three (or maybe even more?) topics: http://www.coderanch.com/t/323647/java/java/enumeratio-or-iterator http://www.coderanch.com/t/399690/java/java/Enumeration http://www.coderanch.com/t/400354/java/java/Difference-Between-Iterator-Enumeration If you have a question about the efficiency of Enumeration vs. Iterator, please open your own topic, otherwise we'll have three different discussions going on on the same topic and it will be difficult to follow. I already gave you an answer in one of the other two topics.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: ??? Difference Between Iterator & Enumeration ???
|
|
|