Enumeration is an interface. It is used in the collection framework in java to retrieve the elements one by one. Enumeration is a legacy interface that is applicable only for legacy classes like Vector, HashTable, Stack, etc. It provides a single direction iteration. By using enumeration, we can perform only read operation, and we cannot perform remove operation.
Enumeration object can be created by calling elements() method present in the Vector class.
// Here "v" is a vector object. enum is of // type Enumeration interface and refers to "v" Enumeration enum = v.elements();
An iterator is a universal cursor that can be applied to any collection object. It provides a single direction iteration. By using an iterator, we can perform both read and remove operation, but we cannot perform replace operation. Iterator must be used whenever we want to enumerate elements in all collection framework implemented interfaces like Set, List, Queue, DeQue, and also implemented classes of Map interface.
Iterator object can be created by calling iterator() method present in the Collection interface
// Here "c" is any collection object. itr is of // type Iterator interface and refers to "c" Iterator itr = c.iterator();
ListIterator is the most powerful cursor among all the three cursors. ListIterator is only applicable for list implemented classes like ArrayList, LinkedList, Stack, etc. ListIterator traverses both in the forward and backward direction. By using ListIteartor, we can perform read, remove, and replace operation. The ListIterator must be used when we want to enumerate elements of the list.
ListIterator object can be created by calling listIterator() method present in the list interface.
// Here "l" is any list object. ltr is of // type ListIterator interface and refers to "l" ListIterator ltr = l.listIterator();
Enumeration: 10 20 30 40 50 Iterator: [20, 30, 40, 50] ListIterator: [30, 60, 100, 50]
Table showing the Difference between Enumeration, Iterator, and ListIterator
|1. Where we can apply?||It can be applied only to the legacy classes.||It can be applied to any collection interface.||It can be applied to the only list interface.|
|2. Is it a legacy?||Yes (introduced in 1.0 V).||No (introduced in 1.2 V).||No (introduced in 1.2 V).|
|3. Allowed Movement||Single direction, i.e we can traverse elements present in the collection only in the forward direction.||Single direction, i.e we can traverse elements present in the collection only in the forward direction.||Bidirectional, i.e we can traverse elements present in the collection both in forward and backward directions.|
|4. Allowed Operation||We can only perform the read operation.||We can perform read and remove operation.||We can perform read, remove, add, and replace operations.|
|5. How can we get it?||By calling elements() method present in the vector class.||By calling iterator() method present in any collection interface.||By calling listIterator() method present in the list interface.|
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Retrieving Elements from Collection in Java (For-each, Iterator, ListIterator & EnumerationIterator)
- Difference between an Iterator and ListIterator in Java
- Difference between Iterator and Enumeration in Java with Examples
- LinkedList listIterator() Method in Java
- Java AbstractSequentialList | ListIterator()
- ArrayList listIterator() method in Java with Examples
- AbstractList listIterator() Method in Java with Examples
- Stack listIterator(int) method in Java with Example
- Stack listIterator() method in Java with Example
- Vector listIterator() method in Java with Examples
- List listIterator() Method in Java with Examples
- ListIterator in Java
- CopyOnWriteArrayList listIterator() method in Java
- Collections enumeration() method in Java with Examples
- Enumeration nextElement() Method in Java with Examples
- Enumeration hasMoreElements() Method in Java with Examples
- Enumeration asIterator() Method in Java with Examples
- How to Use Enumeration to Display Elements of Hashtable in Java?
- Enumeration Interface In Java
- Iterator vs Foreach In Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.