Different using. java.util.Enumeration and Iterator?

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 is improved in such a way so the method names are shorter, and has an additional remove method. Here is a side-by-side comparison: Enumeration Iterator ---------------- ---------------- hasMoreElement() 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.).

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 is improved in such a way so the method names are shorter, and has an additional remove method.

Here is a side-by-side comparison: Enumeration Iterator ---------------- ---------------- hasMoreElement() 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. ).

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.

I used a google search and the first result was an interesting discussion in JavaRanch about Enumeration vs Iterator.

1 +1 for using lmgtfy :) – Andreas_D Jun 4 '09 at 8:03 1 Hehe I could not resist – victor hugo Jun 4 '09 at 8:55.

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.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions