Enumeration asIterator() Method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


References: https://docs.oracle.com/javase/10/docs/api/java/util/Enumeration.html#asIterator()



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.