Preface
In the code example of database connection pool analysis, you can see that Enumeration is used to traverse the Vector collection. Later, I looked for some information to see what methods can be used to traverse collection classes. I found the following examples on the Internet of using Enumeration and Iterator to traverse collection classes. However, this example mentioned that Enumeration is more efficient than Iterator. In fact, this is not the case . The time test in this example is too one-sided because the amount of data is too small. As the number of data increases, the efficiency between the two becomes closer and closer, without the ratio of multiples appearing. Moreover, Iterator is now commonly used to traverse collection classes. Only those that specifically declare that Enumeration must be used will use this class to traverse collections.
Code example
Copy the code code as follows:
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 and create hashtable
Hashtable<String, String> ht = new Hashtable<String, String>();
for (int i = 0; i < 10000; i++) {
ht.put("Key=" + i, "Val=" + i);
}
// 1. Use Enumeration
long start = System.currentTimeMillis();
Enumeration<String> en = ht.keys();//Use enumeration to get key
while (en.hasMoreElements()) {
en.nextElement();
}
long end = System.currentTimeMillis();
System.out.println("Enumeration keys costs " + (end - start)
+ " milliseconds");
// 2. Use Enumeration
start = System.currentTimeMillis();
Enumeration<String> en2 = ht.elements();//Use enumeration to get this 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 an iterator to get this 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 an iterator to get this key-value pair
while (it2.hasNext()) {
it2.next();
}
end = System.currentTimeMillis();
System.out.println("Iterator entrySet costs " + (end - start)
+ " milliseconds");
}
}
Deprecated interface: Enumeration
The Enumeration interface was introduced in JDK 1.0 and is the best iteration output interface. When Vector was first used (ArrayList is now recommended), the Enumeration interface was used for output. Although Enumeration is an old class, the Enumeration class was expanded after JDK1.5 to add generic operation applications.
Commonly used methods of the Enumeration interface include hasMoreElements() (to determine whether there is a next value) and nextElement() (to remove the current element). The functions of these methods are similar to Iterator, except that there is a method to delete data in Iterator, but this interface does not have deletion. operate.
Why continue to use the Enumeration interface?
The Enumeration and Iterator interfaces have similar functions, and Iterator has more functions than Enumeration, so why use Enumeration? This is because the development of Java has gone through a long time, and some older systems or methods in class libraries still use the Enumeration interface. Therefore, for compatibility, Enumeration still needs to be used.
Common subclasses of List interface
Commonly used subclasses of the List interface include ArrayList and Vector. The two have many similarities. A comparison between the two is given below.