Enumeration Interface in Java

Hello Learners, today we are going to learn about the use of Enumeration Interface with collections in Java. The Enumeration interface was introduced in JDK 1.0 under Java.util package. Enumeration is a Legacy Interface so it can be used only with the Stack, Vectors, Hash Tables etc.

Methods of Enumeration Interface in Java

The Enumeration interface offers some methods that can be used to select elements from a collection one by one at a time. The two methods are given below:

  • hasMoreElements(): this method is of boolean type. As the name suggests, if the collection is having more elements then it returns true or else it returns false. When all the elements have been selected or enumerated then it returns false.
  • nextElement(): it returns the next element in the collection. This method is of object type (as an object reference).

See the code below, Observe every statement and try to solve it on your own…

import Java.util.Stack;
import Java.util.ArrayList;
import Java.util.Enumeration;

public class Enumerate {
  
  static Stack<Integer> addToStack(ArrayList<Integer> a) {
    Stack<Integer> s = new Stack<Integer>();
    s.addAll(a); //all elements of arraylist are now added to vectors
    return s;
  }

  public static void main(String[] args) {
    
    ArrayList<Integer> a = new ArrayList<Integer>(10);
    a.add(101);
    a.add(102);
    a.add(103);
    a.add(114);
    a.add(305);
    Stack<Integer> sta = new Stack<Integer>();
    sta = addToStack(a);
    //create an instance of enumeration
    Enumeration<Integer> en = sta.elements();
    int i = 1;
    while(en.hasMoreElements()) {
      System.out.printf("%dth element of stack: ",i);
      System.out.println(en.nextElement()+100);
      i++;
    }
  }
}

OUTPUT:

1th element of stack: 201
2th element of stack: 202
3th element of stack: 203
4th element of stack: 214
5th element of stack: 405
  • On line 7, we have created a method named addToStack which takes an ArrayList of Integer type as an argument. This method adds all the elements of ArrayList into a stack and returns it.
  • On line 21, we defined a stack and called the addToStack method by passing the ArrayList in it. on line 22 we saved the result into the stack only.
  • On line 24, we created an Enumeration instance for the stack.
  • From lines 26-28, we have created a while loop that checks the condition whether the stack has more elements to pick or not, using the hasMoreElements() method.
  • If the stack has more elements then the condition becomes true that element is picked using nextElement() method. Now it enters into the loop and increments the element by 100.
OTHER POINTS TO REMEMBER:
  • Enumerations are Fail-False in nature which means it will not throw any error if you modify any collection while execution.
  • Enumerations are not thread-safe because it allows other threads to modify the collection while traversing.

You can learn more about Stack in Java by clicking on the links.

So, that’s all for now about Enumeration Interface in Java, till then Keep Learning, Keep Practicing, Keep Reading!

“THINK TWICE CODE ONCE!”

Leave a Reply

Your email address will not be published. Required fields are marked *