An object that implements the Enumeration interface generates a series of elements, one at a time. The asIterator() method of Enumeration used to return an Iterator that traverses through the remaining elements covered by this enumeration. Traversal is undefined if any methods are called on this enumeration after the call to asIterator().
Syntax:
default Iterator asIterator()
Parameters: This method accepts nothing.
Return value: This method returns an Iterator representing the remaining elements of this Enumeration.
Below programs illustrate asIterator() method:
Program 1:
// Java program to demonstrate// Enumeration.asIterator() method  import java.util.*;  public class GFG {      public static void main(String[] args)    {          // create enumeration        Enumeration Days;        Vector week = new Vector();          week.add("Sunday");        week.add("Monday");        week.add("Tuesday");        week.add("Wednesday");        week.add("Thursday");        week.add("Friday");        week.add("Saturday");        Days = week.elements();          // get the iterator        Days.asIterator()            .forEachRemaining(s -> System.out.println(s));    }} |
Program 2:
// Java program to demonstrate// Enumeration.asIterator() method  import java.util.*;  public class GFG {      public static void main(String[] args)    {          // create enumeration        Enumeration<Integer> classNine;        Vector<Integer> rollno            = new Vector<Integer>();          rollno.add(1);        rollno.add(2);        rollno.add(3);        rollno.add(4);        rollno.add(5);        classNine = rollno.elements();          // get the iterator        classNine.asIterator()            .forEachRemaining(s -> System.out.println(s));    }} |
References: https://docs.oracle.com/javase/10/docs/api/java/util/Enumeration.html#asIterator()


… [Trackback]
[…] Info on that Topic: geeksforgeeks.org/enumeration-asiterator-method-in-java-with-examples/ […]