java.util.Enumeration interface Examples

0
1906

The Enumeration interface in Java is used to get elements of Collection one by one. Enumeration is a legacy interface of Collection framework and mostly it is used to get series of elements one by one from legacy implementations of Collection and Map. in this article we will see usage of Enumeration interface methods and limitations with examples.

Following are the Legacy characters of Collection framework.

  1. Enumeration (Interface)
  2. Dictionary (Abstract Class)
  3. Vector (Class)
  4. Stack (Class)
  5. Hashtable (Class)
  6. Properties (Class)

1. Java Enumeration interface methods

  1. boolean hasMoreElements() – Tests if this enumeration contains more elements, returns true if contains at least one next element.
  2. E nextElement() – Returns the next element of this enumeration if this enumeration object has at least one more element to provide.

2. Enumeration example with Vector

Following example shows you how to iterate elements of Vector on by one using Enumeration. Vector class provides elements() method to get Enumeration object.

public class JavaVectorEnumerationDemo {

	public static void main(String[] args) {
		
		Vector<String> v = new Vector<>();
		
		// adding single element
		v.add("Peter");//Collection interface method
		v.addElement("Martin");//Vector Specific method
		v.add(2, "Mike");//List specific method
			
		System.out.println(v); //[Peter, Martin, Mike]
		
		// Obtaining enumeration
		Enumeration<String> e = v.elements();
		
		while (e.hasMoreElements()) {
			String string = (String) e.nextElement();
			System.out.println(string);
		}
	}
}

Output :

[Peter, Martin, Mike]
Peter
Martin
Mike

3. Java Enumeration example with Hashtable

Following example shows you how to retrieve elements of Hashtable one by one from Enumeration. Hashtable keys() and elements() methods return Enumeration object.

public class HashTableEnumerationDemo {

	public static void main(String[] args) {
		Hashtable<Integer, String> h = new Hashtable<>();
		h.put(1,"A");
		h.put(2,"B");
		h.put(3,"C");
		h.put(4,"D");
		h.put(5,"E");
		h.put(6,"F");
		h.put(7,"G");
		h.put(8,"H");
		h.put(22,"I");
		h.put(0,"K");
		h.put(23,"J");
		h.put(25,"L");
		System.out.println(h);
		
		// Obtaining keys as enumeration from Hashtable
		Enumeration<Integer> e = h.keys();
		
		while (e.hasMoreElements()) {
			Integer key = (Integer) e.nextElement();
			System.out.println("[key"+key+", value="+h.get(key)+"]");
		}
	}
}

Output :

{22=I, 8=H, 7=G, 6=F, 5=E, 4=D, 3=C, 25=L, 2=B, 1=A, 23=J, 0=K}
[key22, value=I]
[key8, value=H]
[key7, value=G]
[key6, value=F]
[key5, value=E]
[key4, value=D]
[key3, value=C]
[key25, value=L]
[key2, value=B]
[key1, value=A]
[key23, value=J]
[key0, value=K]

4. Limitation of Java Enumeration

  1. We can apply Enumeration only for legacy collection implementations.
  2. Enumeration is only for read operations, we can not perform remove or update the values of collection.

Conclusion

In this article we have covered, what is Enumeration interface in Java collections, and limitations of Enumeraion with examples. To over come these limitations we can use Iterator or ListIterator.

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.

References

  1. Java Documentation

LEAVE A REPLY

Please enter your comment!
Please enter your name here