

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Enumeration in Java
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 superceded by Iterator. Although not deprecated, Enumeration is considered obsolete for new code. However, it is used by several methods defined by the legacy classes such as Vector and Properties, is used by several other API classes, and is currently in widespread use in application code.
The methods declared by Enumeration are summarized in the following table −
Sr.No. | Method & Description |
---|---|
1 | boolean hasMoreElements( ) When implemented, it must return true while there are still more elements to extract, and false when all the elements have been enumerated. |
2 | Object nextElement( ) This returns the next object in the enumeration as a generic Object reference. |
Example
Following is an example showing usage of Enumeration.
import java.util.Vector; import java.util.Enumeration; public class EnumerationTester { public static void main(String args[]) { Enumeration days; Vector dayNames = new Vector(); dayNames.add("Sunday"); dayNames.add("Monday"); dayNames.add("Tuesday"); dayNames.add("Wednesday"); dayNames.add("Thursday"); dayNames.add("Friday"); dayNames.add("Saturday"); days = dayNames.elements(); while (days.hasMoreElements()) { System.out.println(days.nextElement()); } } }
This will produce the following result −
Output
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
- Related Questions & Answers
- Comparing Enumeration Values in Java
- Get Enumeration over HashSet in Java
- Difference between Iterator and Enumeration in Java
- Difference Between Iterator and Enumeration Interface in Java
- Get Enumeration over ArrayList with Java Collections
- Java Program to convert HashSet to Enumeration
- What is enumeration in C#?
- Enumeration of Binary Trees in C++
- Traverse a collection of objects using the Enumeration Interface in Java
- What is the difference between Enumeration interface and enum in Java?
- How an enumeration value in MySQL can be used in an expression?
- C# Program to get the type of the specified Enumeration
- Get the underlying type of the current enumeration type C#
- How to show that each MySQL enumeration has an index value?
- Get the names of the members of the current enumeration type in C#
Advertisements