The enumeration is twice times faster than iterator and consumes less memory at the same time. However, iterator is far more secure than enumeration because other threads are not able to modify objects in the collection that is being traversed by iterator. At the same time, iterator allows the caller to delete elements from the underlying collection, which is not possible for enumeration.
1 PackageJava.util;2 3 Public InterfaceEnumeration<e> {4 Booleanhasmoreelements ();5 E nextelement ();6 }7 Public InterfaceIterator<e> {8 BooleanHasnext ();9 E Next ();Ten voidremove (); One}
In addition:
function interface is different
Enumeration has only 2 function interfaces. with enumeration, we can only read the data of the collection, but not modify the data.
Iterator has only 3 function interfaces. iterator data can be deleted in addition to the data that can be read from the collection.
Iterator supports the fail-fast mechanism, and enumeration does not support
Enumeration is the interface added by JDK 1.0. The functions used to it include vectors, Hashtable, and so on, all of which are added to JDK 1.0, and the purpose of enumeration exists is to provide them with traversal interfaces. The enumeration itself does not support synchronization, and when the vector, Hashtable implements enumeration, synchronization is added.
Iterator is the interface that JDK 1.2 adds, and it also provides traversal interfaces for collections such as HashMap, ArrayList, and so on. Iterator is supported by the FAIL-FAST mechanism: When multiple threads operate on the contents of the same collection, Fail-fast events can occur.
as for rapid failure and safety failure, you can refer to the What is the difference between fast failure (fail-fast) and security failure (fail-safe)? "article
What are the differences between the enumeration interface and the iterator interface?