
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Difference Between Iterator and Enumeration in Java
Iterator and Enumeration are both cursors to traverse and access elements from the collection. They both belong to the collection framework. Enumeration was added in JDK1.0 and Iterator in JDK 1.2 version in the collection framework.
Java Enumeration
Enumeration: An enumeration is a special "class" that indicates a collection of constants. Enumeration can't make structural changes in the collection because it has read-only access to its elements. It has the following methods ?
- hasMoreElements(): The hasMoreElements() method checks to see if more elements exist in the underlying collection class
- nextElement(): The nextElement() method returns the next elements from the collection
Example of Enumeration
The following is the example of Enumeration ?
import java.util.*; public class EnumerationExample { public static void main(String args[]) { List list = new ArrayList(Arrays.asList(new String[] {"Apple", "Cat", "Dog", "Rat"})); Vector v = new Vector(list); delete(v, "Dog"); } private static void delete(Vector v, String name) { Enumeration e = v.elements(); while (e.hasMoreElements()) { String s = (String) e.nextElement(); if (s.equals(name)) { v.remove(name); // This causes ConcurrentModificationException in some JVMs } } // Display the names System.out.println("The names are:"); e = v.elements(); while (e.hasMoreElements()) { System.out.println(e.nextElement()); } } }
The output of the above Java program is ?
The names are: Apple Cat Rat
Java Iterator
Iterator: An iterator can read and remove elements in the collection. It has the following methods ?
- hasNext(): The hasNext() method checks to see if more objects exist in the underlying collection class.
- next(): The next() method returns the next object from the collection.
- remove(): The remove() method removes the last element from the collection that was returned by the next() method.
- forEachRemaining(): The forEachRemaining() method performs a function on all remaining elements in the collection.
Example of Iterator
The following is the example of Iterator ?
import java.util.*; public class IteratorExample { public static void main(String args[]) { List<String> list = new ArrayList<>(Arrays.asList("Apple", "Cat", "Dog", "Rat")); Vector<String> v = new Vector<>(list); delete(v, "Dog"); } private static void delete(Vector<String> v, String name) { Iterator<String> i = v.iterator(); while (i.hasNext()) { String s = i.next(); if (s.equals(name)) { i.remove(); // Safe removal } } // Display the names System.out.println("The names are:"); i = v.iterator(); while (i.hasNext()) { System.out.println(i.next()); } } }
The output of the above Java program is ?
The names are: Apple Cat Rat
Difference between Iterator and Enumeration
The following table shows the difference between Iterator and Enumeration ?
Sr. No. | Key | Iterator | Enumeration |
---|---|---|---|
1 |
Basic |
In Iterator, we can read and remove element while traversing element in the collections. |
Using Enumeration, we can only read element during traversing element in the collections. |
2. |
Access |
It can be used with any class of the collection framework. |
It can be used only with legacy class of the collection framework such as a Vector and HashTable. |
3. |
Fail-Fast and Fail -Safe |
Any changes in the collection, such as removing element from the collection during a thread is iterating collection then it throw concurrent modification exception. |
Enumeration is Fail safe in nature. It doesn't throw concurrent modification exception |
4. |
Limitation |
Only forward direction iterating is possible |
Remove operations can not be performed using Enumeration. |
5. |
Methods |
It has following methods ? *hasNext() *next() *remove() |
It has following methods ? *hasMoreElements() *nextElement() |