简体   繁体   中英

Difference between Java Enumeration and Iterator

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.

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.)

Iterators are .ie 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.

"Officially", they are supposed to be similar with the iterator interface supporting extra operations (eg, 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.

一个简单但在之前的答案中没有提到的事实是Iterator<T>Iterable<T>一起用于解释for(_type_ element:collection){...}结构。

There is basic three difference in Enumeration and Iterator

Enumeration
1. it is use for only lagacy class(eg. Vector )

    Enumeration e = v.elements();  
    v is the object of `Vector` class

2. Read operation can be perform, we can not remove element.
3. Two Method are available

  • public boolean hasNextElement();
  • public Object nextElement();

Iterator

  1. it is applicable for all Collection

    Iterator itr = c.iterator(); where c is any `Collection` class
  2. Read and Remove operation can be perform

  3. Three Method are available

    • public boolean hasNext();
    • public Object next();
    • public void remove();

Limition in both

  • Move only forward direction
  • There is no any methods for Add object and Replace object

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 .

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.

Enumeration 只能用于遗留类(Vector, Stack...),而 Iterator 可以用于所有。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM