Previous
Next
Share This Page On :

java util Enumeration Interface

java.util.Enumeration Interface

Enumeration Interface in Java

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.

Methods are provided to enumerate through the elements of a vector, the keys of a hashtable, and the values in a hashtable. Enumerations are also used to specify the input streams to a SequenceInputStream.

It is possible to adapt an Enumeration to an Iterator by using the asIterator() method.

Note :

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.

In this tutorial you can learn about java.util.Enumeration Interface and its examples. And also learn how to use java.util.Enumeration Interface.

java.util.Enumeration Interface Example

/*  Java Enumeration Interface Example
    Save with file name EnumerationExample.java */
public class EnumerationExample
{
  public static void main(String args[])
  {
    //java.util.Vector DECLARATION
    java.util.Vector v;
    //java.util.Vector OBJECT CREATION
    //USING DEFAULT CONSTRUCTOR
    v = new java.util.Vector();
    //ADD AN ELEMENT
    v.add(new Integer(117));
    //ADD ANOTHER ELEMENT
    v.addElement(new Boolean(false));
    //java.util.Enumeration Usage
    java.util.Enumeration e = v.elements();
    while(e.hasMoreElements())
    {
      System.out.println(e.nextElement());
    }
  }
}

Use of java.util.Enumeration in SequenceInputStream class found in the java.io package. The class has a constructor that accepts an Enumeration.

Following Example will be executed successfully whenever 3 files i.e. file1.txt, file2.txt and file3.txt exists in the current directory of this example.

Create above three files with any text in those files and run the example, the example displays the contents of these 3 files.

How to use Java Enumeration Interface with SequenceInputStream

/*  Java Enumeration Interface Example 2
    java.util.Enumeration interface Example with SequenceInputStream class
    Save with file name EnumerationExample2.java    */
import java.util.Enumeration;
import java.util.Vector;
import java.io.FileInputStream;
import java.io.SequenceInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class EnumerationExample2
{
  public static void main(String args[])
  {
    try
    {
      //java.util.Vector DECLARATION
      Vector v = new Vector(3);
      v.add(new FileInputStream("file1.txt"));
      v.add(new FileInputStream("file2.txt"));
      v.add(new FileInputStream("file3.txt"));
      Enumeration e = v.elements();
      SequenceInputStream sis = new SequenceInputStream(e);
      InputStreamReader isr = new InputStreamReader(sis);
      BufferedReader br = new BufferedReader(isr);
      String line;
      while ((line = br.readLine()) != null)
      {
        System.out.println(line);
      }
      br.close();
    }
    catch(Exception ex)
    {
      System.out.println("Problem Occurred : " + ex);
    }
  }
}

You will learn how to create custom Enumeration from the following example.

How to create custom Enumeration

/*  How to create custom Enumeration Example
    Save with file name ArrayEnumeration.java */
import java.util.Enumeration;
import java.lang.reflect.Array;
public class ArrayEnumeration implements Enumeration
{
  private final int size;
  private int cursor;
  private final Object array;
  public ArrayEnumeration(Object obj)
  {
    Class type = obj.getClass();
    if (!type.isArray())
    {
      throw new IllegalArgumentException("Invalid type: " + type);
    }
    size = Array.getLength(obj);
    array = obj;
  }
  public boolean hasMoreElements()
  {
    return (cursor<size);
  }
  public Object nextElement()
  {
    return Array.get(array, cursor++);
  }
}

In the following Java Enumeration example, you can learn how to create Java Enumeration using String Array

How to create Java Enumeration using String Array

/*  How to create Java Enumeration using String Array Example
    Save with file name EnumerationExample3.java    */
public class EnumerationExample3
{
  public static void main(String args[])
  {
    //String Array Creation
    String str[] = {"String1","String2","String3"};
    //ArrayEnumeration Creation
    ArrayEnumeration ae = new ArrayEnumeration(str);
    //ArrayEnumeration Usage
    while(ae.hasMoreElements())
    {
      System.out.println(ae.nextElement());
    }
  }
}

More Related Tutorials

Previous
Next