Enumeration:
Enumeration is the part of legacy classes that deals with the objects and it comes with java1.1. Enumeration is used to go through vector, hashtable etc.
Enumeration acts as Read only interface, because it has the methods only to traverse and fetch the objects. So Enumeration is used when ever we want to make Collection objects as Read-only.
Enumeration has following methods:
hasMoreElements() — Check whether enumeration contains more elements.
nextElement() — Returns the next element of the enumeration if the enumeration object has at least one more element to provide.
An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement() method return successive elements of the series.
For example, to print all elements of a vector v:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.j2eereference; import java.util.Enumeration; import java.util.Vector; public class EnumerationSample { public static void main(String[] args) { Vector v = new Vector(); v.add("Ankita"); v.add("Agarwal"); Enumeration en=v.elements(); while(en.hasMoreElements()){ System.out.println(en.nextElement()); } } } |
Output
Ankita
Agarwal
Methods are provided to enumerate through the elements of a vector, the keys of a hashtable, and the values in a hashtable.
Iterator:
Iterator is introduced in Collection framework. Iterator is used with loops like while loop, for loop etc.
The java.util.Iterator<E> interface provides for one-way traversal
and
java.util.ListIterator<E> provides two-way traversal.
Iterator<E> is a replacement for the older Enumeration class which was used before collections were added to Java.
Iterator has a remove () method, so that we can manipulate the objects like adding and removing the objects from collection.
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.
Iterator has following methods:
hasNext() — To determine when all the elements in the container have been visited the hasNext() test method is used. True if there are more elements for the iterator.
next() — The next() method advances the iterator and returns the value pointed to by the iterator. The first element is obtained upon the first call to next().
remove() — remove() method of the iterator removes the most recently visited element from the container while keeping the iterator usable.
Iterator Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.j2eereference; import java.util.Iterator; import java.util.Vector; public class IteratorSample { public static void main(String[] args) { Vector v = new Vector(); v.add("Ankita"); v.add("John"); v.add("Peter"); Iterator it = v.iterator(); while (it.hasNext()) { String value = (String) it.next(); System.out.println(value); it.remove(); System.out.println("Vector size is " + v.size()); } } } |
Output
Ankita
Vector size is 2
John
Vector size is 1
Peter
Vector size is 0
Leave a Reply