Enumeration asIterator() Method in Java with Examples
Last Updated :
27 Jun, 2019
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:
import java.util.*;
public class GFG {
public static void main(String[] args)
{
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();
Days.asIterator()
.forEachRemaining(s -> System.out.println(s));
}
}
|
Program 2:
import java.util.*;
public class GFG {
public static void main(String[] args)
{
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();
classNine.asIterator()
.forEachRemaining(s -> System.out.println(s));
}
}
|
References: https://docs.oracle.com/javase/10/docs/api/java/util/Enumeration.html#asIterator()
Share your thoughts in the comments
Please Login to comment...