Friday, September 19, 2025
HomeLanguagesJavaEnumeration asIterator() Method in Java with Examples

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:




// 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()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32303 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11841 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7059 POSTS0 COMMENTS
Thapelo Manthata
6740 POSTS0 COMMENTS
Umr Jansen
6745 POSTS0 COMMENTS