Thursday 8 March 2018

Get Enumeration from Java List

'java.util.Collections' class provides 'enumeration' method to get the enumeration over a list of elements.

Enumeration<String> enumeration = Collections.enumeration(list);

while (enumeration.hasMoreElements()) {
         System.out.println(enumeration.nextElement());
}

Find the below working application.

Test.java
package com.sample.test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class Test {
 public static void main(String args[]) throws Exception {
  String s1 = "Hello";
  String s2 = "World";
  String s3 = "krishna";

  List<String> list = Arrays.asList(s1, s2, s3);

  Enumeration<String> enumeration = Collections.enumeration(list);

  while (enumeration.hasMoreElements()) {
   System.out.println(enumeration.nextElement());
  }

 }
}

Output
Hello
World
Krishna




No comments:

Post a Comment