An Enumeration and an Iterator are used to iterate over the collection class details
- 2020-04-01 02:16:57
- OfStack
preface
In the code instance of the database connection pool analysis, you see that it USES Enumeration to traverse the Vector collection. After looking up some resources to see what methods are available for traversing a collection class, an example of traversing a collection class using Enumeration and Iterator was found on the web.
But in this case, it says that Enumeration is more efficient than Iterator, which is not the case
, the example is too one-sided because the amount of data is too small. With the increase of data two, the efficiency between the two is closer and closer, and the ratio of multiple will not appear. And it is now common to use an Iterator to iterate over collection classes, with only those that explicitly state that they must use an Enumeration.
The code examples
package edu.sjtu.erplab.hash;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
//An instance of traversing a hashtable
public class TraveseHashTable {
public static void main(String[] args) {
//Initializes the creation of hashtable
Hashtable<String, String> ht = new Hashtable<String, String>();
for (int i = 0; i < 10000; i++) {
ht.put("Key=" + i, "Val=" + i);
}
//1. Use the Enumeration
long start = System.currentTimeMillis();
Enumeration<String> en = ht.keys();//Use enumeration to get the key
while (en.hasMoreElements()) {
en.nextElement();
}
long end = System.currentTimeMillis();
System.out.println("Enumeration keys costs " + (end - start)
+ " milliseconds");
//2. Use the Enumeration
start = System.currentTimeMillis();
Enumeration<String> en2 = ht.elements();//Use the enumeration to get the key-value pair
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 the 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 the iterator to get the key-value right
while (it2.hasNext()) {
it2.next();
}
end = System.currentTimeMillis();
System.out.println("Iterator entrySet costs " + (end - start)
+ " milliseconds");
}
}
Obsolete interface: Enumeration
The Enumeration interface, introduced in JDK1.0, is the best iteration output interface, and was first used for Vector (ArrayList is now recommended). Although Enumeration is an old class, it has been expanded since JDK1.5 to include generic operations.
The methods commonly used for the Enumeration interface are hasMoreElements() (to determine if there is a next value) and nextElement() (to fetch the current element). These methods are similar to Iterator, except that there is a method to delete data in Iterator, which does not have a delete operation.
Why continue to use the Enumeration interface
The Enumeration and Iterator interfaces have similar functionality, and the Iterator has more than an Enumeration, so why use an Enumeration? This is because Java has been around for a long time, and some of the older systems or methods in class libraries still use the Enumeration interface, so you still need to use Enumeration for compatibility.
A common subclass of the List interface
Common subclasses of the List interface are ArrayList and Vector, which have many similarities. The comparison between the two is given below
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/20130906171501241.jpg ">