使用Enumeration和Iterator遍歷集合類詳解
前言
在數(shù)據(jù)庫(kù)連接池分析的代碼實(shí)例中,看到其中使用Enumeration來(lái)遍歷Vector集合。后來(lái)就找了一些資料查看都有哪些方法可以遍歷集合類,在網(wǎng)上找到了如下的使用Enumeration和Iterator遍歷集合類的實(shí)例。不過(guò)這個(gè)實(shí)例中提到了Enumeration比Iterator的效率更高,其實(shí)并不是這樣子的,該實(shí)例是的時(shí)間測(cè)試太片面了, 因?yàn)閿?shù)據(jù)量太少。隨著數(shù)據(jù)兩的增加,兩者之間的效率越來(lái)越接近,而不會(huì)出現(xiàn)倍數(shù)的比例。而且現(xiàn)在普遍都使用Iterator來(lái)遍歷集合類,只有特別明確聲明必須使用Enumeration的才會(huì)用該類遍歷集合。
代碼實(shí)例
package edu.sjtu.erplab.hash;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
//一個(gè)遍歷hashtable實(shí)例
public class TraveseHashTable {
public static void main(String[] args) {
//初始化創(chuàng)建hashtable
Hashtable<String, String> ht = new Hashtable<String, String>();
for (int i = 0; i < 10000; i++) {
ht.put("Key=" + i, "Val=" + i);
}
// 1. 使用Enumeration
long start = System.currentTimeMillis();
Enumeration<String> en = ht.keys();//使用枚舉獲取key
while (en.hasMoreElements()) {
en.nextElement();
}
long end = System.currentTimeMillis();
System.out.println("Enumeration keys costs " + (end - start)
+ " milliseconds");
// 2. 使用Enumeration
start = System.currentTimeMillis();
Enumeration<String> en2 = ht.elements();//使用枚舉獲取這個(gè)key-value對(duì)
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();//使用迭代器獲取這個(gè)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();//使用迭代器獲取這個(gè)key-value對(duì)
while (it2.hasNext()) {
it2.next();
}
end = System.currentTimeMillis();
System.out.println("Iterator entrySet costs " + (end - start)
+ " milliseconds");
}
}
廢棄的接口:Enumeration
Enumeration接口是JDK1.0時(shí)推出的,是最好的迭代輸出接口,最早使用Vector(現(xiàn)在推薦使用ArrayList)時(shí)就是使用Enumeration接口進(jìn)行輸出。雖然Enumeration是一個(gè)舊的類,但是在JDK1.5之后為Enumeration類進(jìn)行了擴(kuò)充,增加了泛型的操作應(yīng)用。
Enumeration接口常用的方法有hasMoreElements()(判斷是否有下一個(gè)值)和 nextElement()(取出當(dāng)前元素),這些方法的功能跟Iterator類似,只是Iterator中存在刪除數(shù)據(jù)的方法,而此接口不存在刪除操作。
為什么還要繼續(xù)使用Enumeration接口
Enumeration和Iterator接口功能相似,而且Iterator的功能還比Enumeration多,那么為什么還要使用Enumeration?這是因?yàn)閖ava的發(fā)展經(jīng)歷了很長(zhǎng)時(shí)間,一些比較古老的系統(tǒng)或者類庫(kù)中的方法還在使用Enumeration接口,因此為了兼容,還是需要使用Enumeration。
List接口的常用子類
List接口常用的子類有ArrayList和Vector,兩者有許多相似的地方,下面給出這兩者之間的比較
- Java中的collection集合類型總結(jié)
- 集合類List與Dictonary實(shí)例練習(xí)
- js正則函數(shù)match、exec、test、search、replace、split使用介紹集合
- SQL集合函數(shù)中case when then 使用技巧
- Python中集合類型(set)學(xué)習(xí)小結(jié)
- Python set集合類型操作總結(jié)
- C#中DataSet轉(zhuǎn)化為實(shí)體集合類的方法
- 集合類Array List HashTable實(shí)例操作練習(xí)
- Swift教程之集合類型詳解
- C++簡(jiǎn)單集合類的實(shí)現(xiàn)方法
相關(guān)文章
SpringBoot下使用自定義監(jiān)聽事件的流程分析
事件機(jī)制是Spring的一個(gè)功能,目前我們使用了SpringBoot框架,所以記錄下事件機(jī)制在SpringBoot框架下的使用,同時(shí)實(shí)現(xiàn)異步處理,這篇文章主要介紹了SpringBoot下使用自定義監(jiān)聽事件,需要的朋友可以參考下2023-08-08解決javaWEB中前后臺(tái)中文亂碼問(wèn)題的3種方法
這篇文章主要介紹了解決javaWEB中前后臺(tái)中文亂碼問(wèn)題的3種方法,中文問(wèn)題一直是很多人難以解決的問(wèn)題,對(duì)這方面感興趣的朋友可以參考一下2015-11-11SpringBoot中支持Https協(xié)議的實(shí)現(xiàn)
本文主要介紹了SpringBoot中支持Https協(xié)議的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01關(guān)于Spring BeanPostProcessor的執(zhí)行順序
這篇文章主要介紹了Spring BeanPostProcessor的執(zhí)行順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10