Get Enumeration over ArrayList with Java Collections

Java 8Object Oriented ProgrammingProgramming

In order to get enumeration over ArrayList with Java Collections, we use the java.util.Collections.enumeration() method.

Declaration −The java.util.Collections.enumeration() method is declared is as follows −

public static <T> Enumeration<T> enumeration(Collection<T> c)

where c is the collection object for which an enumeration is returned

Let us see a program to get enumeration over ArrayList −

Example

 Live Demo

import java.util.*;
public class Example {
   public static void main (String[] args) {
      ArrayList<Integer> list = new ArrayList<Integer>();
      list.add(14);
      list.add(2);
      list.add(73);
      Enumeration en = Collections.enumeration(list); // getting enumeration over ArrayList list
      while(en.hasMoreElements()) {
         System.out.println(en.nextElement());
      }
   }
}

Output

14
2
73
raja
Published on 08-Jan-2019 13:06:21
Advertisements