What are the differences between the enumeration interface and the iterator interface?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.