What is the exact difference between these two interfaces? Does Enumeration
have benefits over using Iterator
? If anyone could elaborate, a reference article would be appreciated.

- 90,524
- 13
- 150
- 263

- 35,071
- 77
- 215
- 324
-
4I used a google search and the first result was an interesting discussion in JavaRanch about [Enumeration vs Iterator](http://www.coderanch.com/t/202139/Performance/java/Enumeration-vs-Iterator) – victor hugo Jun 04 '09 at 01:41
10 Answers
Looking at the Java API Specification for the Iterator
interface, there is an explanation of the differences between Enumeration
:
Iterators differ from enumerations in two ways:
- Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
- Method names have been improved.
The bottom line is, both Enumeration
and Iterator
will give successive elements, but Iterator
improved the method names by shortening away the verbiage, and it has an additional remove
method. Here is a side-by-side comparison:
Enumeration Iterator
---------------- ----------------
hasMoreElements() hasNext()
nextElement() next()
N/A remove()
As also mentioned in the Java API Specifications, for newer programs, Iterator
should be preferred over Enumeration
, as "Iterator takes the place of Enumeration in the Java collections framework." (From the Iterator
specifications.)
-
9I think there is a bit of explanation missing from this answer regarding concurrency. – Maarten Bodewes Jul 24 '12 at 16:01
-
@Paul_Draper: Edits should not be adding new meaning to the post, that is what comments are for. – Emil Nov 11 '12 at 17:40
-
4@coobird Are you sure "Enumerations are typically faster" ? since Enumeration has "synchronizing block of code inside the nextElement()" And We don't have Synchronization at Iterators which causes ConcurrentModificationException rit?? Do we call Iterators are typically faster and Enumerations are a little safer. ?? – Kanagavelu Sugumar Mar 04 '13 at 08:39
-
@KanagaveluSugumar Thank you for pointing that out. (I didn't notice that the additional discussion was added to this answer.) I have rolled back the edit, as it wasn't entirely accurate. – coobird Mar 05 '13 at 15:02
-
I think it's worth pointing out that remove() is an optional method on the Iterator interface and quite a lot of implementating classes don't implement it. – Kutzi Jul 27 '14 at 09:22
-
1
-
-
-
@rocky: Because they didn't think to add it initially, considered it too much trouble, or didn't have a good design for it yet. Most collections you iterate don't have obvious/easy ways to enable removal of elements and/or allowing removal would hide inefficiencies (e.g. when iterating an `ArrayList`, it would be inefficient to allow element-by-element removal; each removal would be `O(n)`). Presumably when they first implemented iteration, they decided to stick with universal features; by the time they came up with a better system, back-compat constraints prevented improving `Enumeration`. – ShadowRanger Apr 12 '22 at 17:35
Iterators are fail-fast . i.e. when one thread changes the collection by add / remove operations , while another thread is traversing it through an Iterator using hasNext() or next()
method, the iterator fails quickly by throwing ConcurrentModificationException
. The fail-fast behavior of iterators can be used only to detect bugs. The Enumerations returned by the methods of classes like Hashtable, Vector are not fail-fast that is achieved by synchronizing the block of code inside the nextElement()
method that locks the current Vector object which costs lots of time.

- 2,050
- 3
- 21
- 40
-
12Only partly true: this behaviour is not defined in the interface, it is up to the implementation of the Iterator. It's true that the 'old' collection implementations in java.util (HashSet, ArrayList etc.) exhibit this behaviour. However, the newer 'concurrent' collections will never throw a ConcurrentModificationException, they will traverse the collection as of the time of creation of the iterator. Other implementations may show still different behaviour. – Kutzi Jul 27 '14 at 09:27
-
1Also worth pointing out: "Note that fail-fast behavior cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast operations throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: ConcurrentModificationException should be used only to detect bugs." http://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html – Kutzi Jul 27 '14 at 09:32
"Officially", they are supposed to be similar with the iterator interface supporting extra operations (e.g., removal). Generally, the tendency is to use iterators.
Here is from the enumeration interface javadocs:
NOTE: 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.

- 88,451
- 51
- 221
- 321
One simple fact but haven't mentioned in previous answers is that Iterator<T>
is used with Iterable<T>
to serve in interpreting for(_type_ element:collection){...}
structure.

- 10,048
- 5
- 48
- 78
There are three differences between Enumeration
and Iterator
:
Enumeration
It is used for legacy classes only (e.g.
Vector
)// v is an object of the Vector class Enumeration e = v.elements();
We can not remove elements, the collection itself cannot be altered; elements can only be accessed
the following two methods are available:
public boolean hasNextElement()
public Object nextElement()
Iterator
It is applicable for all collections in the Collection framework:
// c is any Collection classIterator itr = c.iterator();
It is possible both to iterate and access the elements, and the elements can be removed.
Three Method are available
public boolean hasNext()
public Object next()
public void remove()
Limitation for both Enumeration
and Iterator
:
- The can only move in the forward direction;
- There are no methods to add or replace an element.

- 90,524
- 13
- 150
- 263

- 3,686
- 16
- 35
1) The main difference between Iterator and Enumeration is removal of the element while traversing the collection. Iterator can remove the element during traversal of collection as it has remove() method. Enumeration does not have remove() method.
2) Enumeration is fail-safe in nature. It does not throw ConcurrentModificationException if Collection is modified during the traversal. Iterator is fail-fast in nature. It throws ConcurrentModificationException if a Collection is modified while iterating other than its own remove() method.
3) Enumeration is a legacy interface which is used for traversing Vector, Hashtable. Iterator is not a legacy interface. Iterator can be used for the traversal of HashMap, LinkedList, ArrayList, HashSet, TreeMap, TreeSet .

- 737
- 8
- 16
If you're writing your own collection class, and you're extending any of the existing classes or implementing any of the Collections framework interfaces, you basically have no choice but to use Iterator.
If for some reason (that I can't think of) you're creating a custom collection class that does not relate to java.util.Collection or java.util.Map in any way, you should still implement Iterable so people can use your class in for loops.

- 1,048
- 6
- 10
The main different is Enumeration doesn't expose remove() method. Moreover, Iterator don't allow a simultaneously navigation and modification on an underlying object. They have a control to see if there are concurrent modifications or so, and hence takes more processing. So Enumeration's performance is virtually 50% faster than Iterator. If we need only navigation ignoring such a synchronization, just use Enumeration.

- 6,048
- 5
- 30
- 39
-
It is true that Enumeration does "not" expose remove() method - but it also does not pay attention to the invocation of Collection's remove() api. For example following code will just print: AAA, CCC, EEE. ----------------------------------------------------- Vector
v=new Vector – javauser71 Jun 14 '13 at 00:06(6); v.add("AAA"); v.add("BBB"); v.add("CCC"); v.add("DDD"); v.add("EEE"); v.add("FFF"); Enumeration en = v.elements(); while(en.hasMoreElements()) String value=(String) en.nextElement(); System.out.println(value); v.remove(value);
Enumeration can be used only for the legacy class(Vector, Stack...), while Iterator can be used for all.

- 17
- 2
Both iterator and enumeration are used to retrieve the data, the difference is that enumeration can be used only for legacy classes i.e vector/stack whereas iterators can be used for the rest. Enumeration can also be used for the key set in maps.

- 58,613
- 19
- 146
- 147