Comparison of enumeration and iterator

Source: Internet
Author: User

Comparison of enumeration and iterator

Enumeration interface

Iterator interface

Meaning of the parameter

Enum type

Iterator element type

Package

Java.util

Parent class

No

Sub-class

StringTokenizer

Beancontextsupport.bcsiterator,

Eventreaderdelegate,scanner

Difference

An object that implements the enumeration interface,

It generates a series of elements, one at a time

A. Successive calls to the Nextelement method will

Returns a series of contiguous elements.

Iterators

Method

Determine if there is a next element

hasMoreElements () tests whether this enumeration

contains more elements.

Hasnext () If there are still elements that can iterate,

Returns True.

Get element

Nextelement () If this enumeration object is at least

There is also an element that can be supplied, then return this

The next element of the enumeration.

Next () returns the next element of the iteration.

Removed from

Remove () from the collection that the iterator points to

The last element returned by the iterator in the remove

(optional operation).

Objective
In the code instance of the database connection pool analysis, you see that you use enumeration to traverse the vector collection. And then I found some information. See what methods you can use to iterate through the collection class, and find the following instances of traversing the collection class using enumeration and iterator on the web. However, this example mentions that enumeration is more efficient than iterator, in fact it is not the case, the time test is too one-sided, because the amount of data is too small. As the data increases, the efficiency of the two becomes more and more close, without a multiplier ratio. And now it is common to use iterator to iterate through the collection class, and only specifically explicitly declare that you must use enumeration to iterate through the collection with that class.


Package Edu.sjtu.erplab.hash;

Import java.util.Enumeration;
Import java.util.Hashtable;
Import Java.util.Iterator;
Import Java.util.Map.Entry;

A traversal Hashtable instance
public class Travesehashtable {
public static void Main (string[] args) {
Initialize Create Hashtable
Hashtable<string, string> ht = new hashtable<string, string> ();
for (int i = 0; i < 10000; i++) {
Ht.put ("key=" + I, "val=" + i);
}

1. Using enumeration
Long start = System.currenttimemillis ();
Enumeration<string> en = Ht.keys ();//Use enumerations to get key
while (En.hasmoreelements ()) {
En.nextelement ();
}
Long end = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Enumeration keys costs" + (End-start)
+ "milliseconds");

2. Using enumeration
Start = System.currenttimemillis ();
enumeration<string> en2 = ht.elements ();//Use enumerations to get this Key-value
while (En2.hasmoreelements ()) {
En2.nextelement ();
}
End = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("enumeration elements costs" + (End-start)
+ "milliseconds");

3. Iterator
Start = System.currenttimemillis ();
Iterator<string> it = Ht.keyset (). Iterator ();//Use an iterator to get the key
while (It.hasnext ()) {
It.next ();
}
End = System.currenttimemillis ();
System.out.println ("Iterator keySet costs" + (End-start)
+ "milliseconds");

4. Iterator
Start = System.currenttimemillis ();
iterator<entry<string, string>> it2 = Ht.entryset (). Iterator ();//Use iterators to get this key-value
while (It2.hasnext ()) {
It2.next ();
}
End = System.currenttimemillis ();
System.out.println ("Iterator entryset costs" + (End-start)
+ "milliseconds");

}
}


Obsolete interface: Enumeration
The enumeration interface is JDK1.0 when it is the best iterative output interface, the earliest use of vectors (now recommended to use ArrayList) is to use the enumeration interface for output. Although enumeration is an old class, it expands the enumeration class after JDK1.5, adding generic operational applications.

Enumeration interface commonly used methods have hasMoreElements () (To determine whether there is a next value) and Nextelement () (take out the current element), these methods are similar to iterator, but there is a way to delete data in iterator, There is no delete operation for this interface.

Why continue to use the enumeration interface
Enumeration and iterator interface functions are similar, and iterator has more features than enumeration, so why use enumeration? This is because Java has evolved over a long period of time, and some older systems or methods in the class library are using the enumeration interface, so for compatibility, you still need to use enumeration.

Common subclasses of the list interface
The common subclasses of the list interface are ArrayList and vectors, and there are many similarities between the two, and the comparison between the two is given below.

Comparison of enumeration and iterator

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.