Java Enumeration and Iterators
What and how to use Enumerationa and Iterators
Defenitions:
Set: an unordered array of elements or objects
Collection: an ordered set
*Vectors already implement the Collection interface, but you can define your own Collection.
*ArrayLists and Vectors, both support Iterators
Enumeration
An enumeration is an object that generates elements one at a time, used for passing through a collection, usually of unknown size.
The traversing of elements can only be done once per creation.
Enumeration’s have two options:
nextElement() which returns the next object in the collection
hasMoreElements() which returns true, until the last object has been returned by nextElement()
Code Example:
import java.util.Vector;
import java.util.Enumeration;
public class EnumerationTester {
public static void main(String args[]) {
Enumeration days;
Vector dayNames = new Vector();
dayNames.add(“Sunday”);
dayNames.add(“Monday”);
dayNames.add(“Tuesday”);
dayNames.add(“Wednesday”);
dayNames.add(“Thursday”);
dayNames.add(“Friday”);
dayNames.add(“Saturday”};
days = dayNames.elements();
while (days.hasMoreElements())
System.out.println(days.nextElement()); } }
Enumerations do not allow for the modification of the collection, which is being traversed, thus the Iterators are used if this is required.
Iterators have 3 options:
hasNext() returns true if there is another element in the collection
next() which returns the next object
remove() which removes the last object taken using next()
Code Example:
import java.util.Vector;
import java.util.Iterator;
public class IteratorTester {
public static void main(String args[]) {
String[] num = {“one”, “two”, “three”, “four”, “five”,”six”, “seven”, “eight”, “nine”, “ten”};
Vector aVector = new
Vector(java.util.Arrays.asList(num));
System.out.println(“Before Vector: ” + aVector);
Iterator nums = aVector.iterator();
while(nums.hasNext()) {
String aString = (String)nums.next();
System.out.println(aString);
if (aString.length() > 4)
nums.remove(); }
System.out.println(“After Vector: ” + aVector); } }
*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
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
Questions/Comments as always can be sent to: william_a_wilson@hotmail.com
*OR suggestions for other Java recipes :)
-William. ยง (marvin_gohan)









Mahendra Sahu said on October 7, 2008
Simple Do this in order to get the size of the Enumeration
Enumeration e = null; //keep your values
System.out.println(“Size of the Enum is:”+Collections.list(e).size());
gunjan garg said on January 31, 2009
what is the difference in Enumeration and Iterator?if they are same then why we read these two different concepts
Lahar said on February 17, 2009
Enumeration is same as Iterator.
and working of both are same but Main differnce is Iterator has remove().
remove() removes the last object taken using next().
swetha said on April 27, 2009
Very Usefull and clear explanation
Anonymous said on July 21, 2009
v.hasNext() and v.hasMoreElements() are not equal to 1 operation, each.
If you break it down to CPU cycles needed to run those functions, I guarantee you that they are AT LEAST as many CPU cycles as a for loop.
Moreover, the compiler might simplify things like “int i = 0″ to one operation.
Nevor said on August 24, 2009
Method calls are clearly not “1 operation” and furthermore you forget about the item.next() which is called at each loop. Thus, with a collection that has a O(1) get size, the for loop is more efficient than the iterator.
Nevor said on August 24, 2009
and a O(1) random access
rgz said on September 17, 2009
I just figured out that we can do this for exactly the same cost and the intention read a bit more clearly.
for(Enumeration e = foos.getEnumeration(); e.hasMoreElements(); Foo foo = e.nextElement()){
// process foo
}
Anonymous said on January 6, 2010
Hi Dear,
Can U send me the Java Interview Qustion and Answers
Thanks in Advance
Narayana
Anonymous said on January 8, 2010
tae k aman .. kamukha mo pwet ko gagu
Anuj Garg said on January 20, 2011
NIce article good one..
sanjeeva said on February 17, 2011
very useful and clear explanation
Sowmya Geeduppala said on March 9, 2011
what is the differene between vector and list and vector and set .
Dilvinriyo said on July 25, 2011
Good Suggetions………..
Wowmittalswati said on August 3, 2011
thanks for clearing facts