Home  >  Core Java

Enumeration Java Example

By Arvind Rai, August 16, 2013
java.util.Enumeration interface in java helps to iterate the vector. The class which implements Enumeration can be iterated using Enumeration. There are two methods hasMoreElements() and nextElement(). thasMoreElements() checks if there is any more values in the vector. nextElement() gets the next value. Find the example to use Enumeration.

EnumerationDemo.java
package com.util;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
public class EnumerationDemo {
    public static void main(String[] args) {
        Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
        ht.put(1,"A");
        ht.put(2,"B");
        ht.put(3,"C");
        //iterate through Enumeration
         Enumeration<Integer> enm = ht.keys();
         while(enm.hasMoreElements()){
             System.out.println(ht.get(enm.nextElement()));
         }
         Vector<String> vec = new Vector<String>();
         vec.add("A");
         vec.add("B");
         vec.add("C");
         //iterate through Enumeration
         Enumeration<String> en = vec.elements();
         while(en.hasMoreElements()){
             System.out.println(en.nextElement());
         }
    }
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us