Enumeration in Java

Enumeration in Java

Enumeration is a legacy interface in Java. It is using to iterate elements in legacy collections like Hashtable .This discussion gives a basic overview on enumeration in Java

Enumeraton Vs Iterator

Both are interfaces in Java for iterating data in a collections .Iterator is the improved version of Enumeration.The method names are shorter in case of Iterator.And one remove() method has been added  in Iterator interface.

Methods in Enumeration

1)boolean hasMoreElements()

2)E nextElement()

Methods in Iterator

1) boolean hasNext()

2) E next()

3) void remove()

Iterating Vector using Enumeration

Let us see how the legacy class Vector is iterating using Enumeration.In our example we are storing a number of Strings in our Vector.Then iterating them using an Enumeration object.

import java.util.Enumeration;
import java.util.Vector;
public class EnumerationSample {
private Vector vector = null;
private Enumeration enumeration = null;
public EnumerationSample() {
vector = new Vector();
}
public void addItems() {
vector.add("cat");
vector.add("cow");
vector.add("sheep");
vector.add("dog");
System.out.println("Added items to vector");
}
public void showItems() {
System.out.println("Now iterating using Enumeration..");
enumeration = vector.elements();
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
}
public static void main(String[] args) {
EnumerationSample sample = new EnumerationSample();
sample.addItems();
sample.showItems();
}
}

Let us verify the output

Output

Added items to vector
Now iterating using Enumeration..
cat
cow
sheep
dog

Iterating Hashtable using Enumeration

Now let us see how a Hashtable is iterating using Enumeration. Here ,we are using  Integer objects as keys and String as values.After inserting data as key – value pair, an Enumeration object is using to  get the  key values. Then displaying the value mapped with each key.

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
public class HashTableE {
private Map<Integer, String> map = null;
private Enumeration enumeration = null;
public HashTableE() {
map = new Hashtable<Integer, String>();
}
public void addItems() {
map.put(0, "cat");
map.put(1, "cow");
map.put(2, "sheep");
map.put(3, "dog");
System.out.println("Added key value pairs");
}
public void displayItems() {
enumeration = ((Hashtable) map).keys();
System.out.println("Keys in Enumeration are : ");
while (enumeration.hasMoreElements()) {
Integer number = (Integer) enumeration.nextElement();
System.out.println("Key = " + number);
System.out.println("value = " + map.get(number));
}
}
public static void main(String[] args) {
HashTableE sample = new HashTableE();
sample.addItems();
sample.displayItems();
}
}

Let us see the output.

Output

Added key value pairs
Keys in Enumeration are :
Key = 3
value = dog
Key = 2
value = sheep
Key = 1
value = cow
Key = 0
value = cat

The two examples discussed  above  gives a clear idea about Enumeration interface.

See Related Discussions:

Collections in Java

Iterator in Java

Threading in Java