

- 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
Retrieving Elements from Collection in Java- EnumerationIterator
EnumerationIterator doesn’t have the option of eliminating elements from the collection, whereas an iterator has this facility. An additional disadvantage of using EnumerationIterator is that the name of methods associated with EnumerationIterator is difficult to remember.
Example
Following is an example −
import java.util.Vector; import java.util.Enumeration; public class Demo { public static void main(String args[]) { Vector day_name = new Vector(); day_name.add("Tuesday"); day_name.add("Thursday"); day_name.add("Saturday"); day_name.add("Sunday"); Enumeration my_days = day_name.elements(); System.out.println("The values are "); while (my_days.hasMoreElements()) System.out.println(my_days.nextElement()); } }
Output
The values are Tuesday Thursday Saturday Sunday
A class named Demo contains the main function, where a Vector instance is defined. Elements are added to the vector with the help of the ‘add’ function. Now, an Enumeration instance is created, and the vector is accessed and the function ‘elements’ is called on it. The output is printed on the screen by iterating over every element and checking if there are any more values in the vector.
- Related Questions & Answers
- Retrieving Elements from Collection in Java- ListIterator
- Retrieving Elements from Collection in Java- Iterator
- Retrieving Elements from Collection in C#
- Retrieving Elements from Collection in Java- For-each loop
- Retrieving specific documents from collection by _id in MongoDB
- Retrieving the first document in a MongoDB collection?
- Retrieving MySQL Database structure information from Java?
- How to retain elements from a Collection in another Collection
- How to limit the number of records, while retrieving data from a MongoDB collection using Java?
- How to remove all elements from a Collection in another Collection
- Remove all elements from the Collection in C#
- Rotate elements of a collection in Java
- Add elements to LinkedHashMap collection in Java
- Add all the elements from a collection to the HashSet in Java
- Java Program to Compare Elements in a Collection
Advertisements