The difference between iterators and enumerations

Source: Internet
Author: User

1, iterators allow callers to remove elements from the underlying collection using well-defined semantics during the iteration .

2, method name has been improved

Iterator interface, and enumeration is similar, but the name is relatively short, usually recommended with Iterator
An iterator that iterates over the collection. Iterators replace the enumeration in the Java collections Framework. The iterator method is defined in the collections interface to return iterators that iterate over the elements of this collection.

iterator is a collection of iterators, and the iterator provider does not care about the implementation of the collection.
three methods are defined in the iterator interface:
1.hasNext ()
whether there is a next element.
2.next ()
returns the next element.
3.remove ()
deletes the current element.
only three simple methods are defined, here are a few things to note:
1) The difference between iterator and enumeration
iterator is used to replace enumeration, enumeration only defines two methods and does not have a delete function.
2) The Remove () method can be called after the next () method is called, and a IllegalStateException exception is thrown if the remove () method is called only once per call to next ().

The enumeration enumeration interface is actually an interface that is earlier than the Iterator iterator interface to enumerate elements in the collection.
Although enumeration may be obsolete and replaced by Iterator, many servlets are used, so there is a need to learn.

The enumeration (enumeration) interface is inherited from a previous version.   Enumeration and each of the classes left from previous versions are described in turn below. 
The enumeration interface defines a method that can enumerate elements of an object's class set (one at a time). This interface, though not abandoned, has been replaced by iterator. Enumeration is outdated for the new program. However, it is still used by several of the methods defined by the previous versions of classes (such as vectors and properties), used by several other API classes, and used by applications that are currently widely used.
enumeration Specifies the following two methods:
boolean hasmoreelements ()
Object nextelement ()
after execution, the hasMoreElements () method must return True when there are still more elements to extract. Returns False when all elements are enumerated. The Nextelement () method returns the next object in the enumeration as a reference to a generic object. That is, each call to the Nextelement () method gets the next object in the enumeration. The calling routine must set that object to the object type contained within the enumeration.
For enumeration can take vector as an example
There are many objects in the vector, if you want to see all of them, one way is to use the vector get (int index) method, but this is less efficient, and the other method is to use vector elements () The enumeration method returns an object that is moved down with the enumeration hasMoreElements () method and determines whether there is an object at the current position, and then returns the object with the Nextelement () method.
For example, print all objects in vector V:
Enumeration E = v.elements ()
While (E.hasmoreelements ())
{
System.out.println (E.nextelement ());
}

The difference between iterators and enumerations

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.