Enumeration Interface in Java Framework

Enumeration Interface

Implementing Enumeration interface generates the series of elements, one by one.

The Enumeration interface has two methods.

1. boolean hasMoreElements( ) : It must return true if this enumeration contains more elements.

2. Object nextElement( ) : It returns the next element of this enumeration object.

Example: Program to implement methods available in Enumeration interface


import java.util.Vector;
import java.util.Enumeration;
public class EnumDemo
{
      public static void main(String args[])
      {
            Enumeration data;
            Vector<String> vec = new Vector<String>();
            vec.add("AA");
            vec.add("BB");
            vec.add("CC");
            vec.add("DD");
            data = vec.elements();
            while (data.hasMoreElements())
            {
                  System.out.println (data.nextElement());
            }
      }
}


Output:
AA
BB
CC
DD

Comparable Interface

  • The Comparable interface provides order to the object of each class. This interface is available in java.lang package.
  • It has only compareTo () method.
Syntax:
public interface Comparable <T>

Comparator Interface

The Comparator interface provides an order for object. This interface is available in java.util package. It can be also used to control the order of certain data structures.

It has two methods.
1. public int compare(Object ob1, Object ob2)
2. public boolean equals(Object obj)