Previously we have discussed about
Here we learn about Enumeration Interface In Java, but we must also know What is Iterator Interface In Java ?
Interface Enumeration<E>
All Known Subinterfaces:
NamingEnumeration<T>
All Known Implementing Classes:
StringTokenizer
Declaration of Enumeration:
public interface Enumeration<E>
The Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.This legacy interface has been superseded by Iterator. Although not deprecated.
Methods provided by Enumeration are enumerate through the elements of a vector, the keys of a hashtable, and the values in a hashtable. Enumerations are also used to specify the input streams to a SequenceInputStream.
The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.
Method Summary of Enumeration:
Modifier and Method Name
|
Description
|
boolean hasMoreElements()
|
Tests if this enumeration contains more elements.
|
E nextElement()
|
Returns the next element of this enumeration if this enumeration
object has at least one more element to provide.
|
Example of Enumeration:
import java.util.Enumeration;
import java.util.Vector;
public class EnumerationInterfaceExample {
public static void main(String[] args) {
Vector v = new Vector();
v.add(50);
v.add(55);
v.add(60);
v.add(65);
v.add(70);
Enumeration en = v.elements();
while(en.hasMoreElements()){
System.out.println(en.nextElement());
}
System.out.println("\nUsing Enumeration with For loop : ");
for(Enumeration e = v.elements();e.hasMoreElements();){
System.out.println(e.nextElement());
}
}
}
Program Output:
50
55
60
65
70
Using Enumeration with For loop :
50
55
60
65
70
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.


No comments:
Post a Comment