enumeration与itertor
2015-09-08 22:32
330 查看
原文:http://www.jb51.net/article/41183.htm
代码如下:
package edu.sjtu.erplab.hash;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
//一个遍历hashtable实例
public class TraveseHashTable {
public static void main(String[] args) {
//初始化创建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();//使用枚举获取这个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();//使用迭代器获取这个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();//使用迭代器获取这个key-value对
while (it2.hasNext()) {
it2.next();
}
end = System.currentTimeMillis();
System.out.println("Iterator entrySet costs " + (end - start)
+ " milliseconds");
}
}
废弃的接口:Enumeration
Enumeration接口是JDK1.0时推出的,是最好的迭代输出接口,最早使用Vector(现在推荐使用ArrayList)时就是使用Enumeration接口进行输出。虽然Enumeration是一个旧的类,但是在JDK1.5之后为Enumeration类进行了扩充,增加了泛型的操作应用。
Enumeration接口常用的方法有hasMoreElements()(判断是否有下一个值)和 nextElement()(取出当前元素),这些方法的功能跟Iterator类似,只是Iterator中存在删除数据的方法,而此接口不存在删除操作。
为什么还要继续使用Enumeration接口
Enumeration和Iterator接口功能相似,而且Iterator的功能还比Enumeration多,那么为什么还要使用Enumeration?这是因为java的发展经历了很长时间,一些比较古老的系统或者类库中的方法还在使用Enumeration接口,因此为了兼容,还是需要使用Enumeration。
List接口的常用子类
List接口常用的子类有ArrayList和Vector,两者有许多相似的地方,下面给出这两者之间的比较
代码如下:
package edu.sjtu.erplab.hash;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
//一个遍历hashtable实例
public class TraveseHashTable {
public static void main(String[] args) {
//初始化创建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();//使用枚举获取这个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();//使用迭代器获取这个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();//使用迭代器获取这个key-value对
while (it2.hasNext()) {
it2.next();
}
end = System.currentTimeMillis();
System.out.println("Iterator entrySet costs " + (end - start)
+ " milliseconds");
}
}
废弃的接口:Enumeration
Enumeration接口是JDK1.0时推出的,是最好的迭代输出接口,最早使用Vector(现在推荐使用ArrayList)时就是使用Enumeration接口进行输出。虽然Enumeration是一个旧的类,但是在JDK1.5之后为Enumeration类进行了扩充,增加了泛型的操作应用。
Enumeration接口常用的方法有hasMoreElements()(判断是否有下一个值)和 nextElement()(取出当前元素),这些方法的功能跟Iterator类似,只是Iterator中存在删除数据的方法,而此接口不存在删除操作。
为什么还要继续使用Enumeration接口
Enumeration和Iterator接口功能相似,而且Iterator的功能还比Enumeration多,那么为什么还要使用Enumeration?这是因为java的发展经历了很长时间,一些比较古老的系统或者类库中的方法还在使用Enumeration接口,因此为了兼容,还是需要使用Enumeration。
List接口的常用子类
List接口常用的子类有ArrayList和Vector,两者有许多相似的地方,下面给出这两者之间的比较
![](http://files.jb51.net/file_images/article/201309/20130906171501241.jpg)
相关文章推荐
- CSDN-markdown编辑器
- 15_09_08 返回值类型错误
- Python time模块函数与时间格式关系图解
- 【J2SE】——基础宏总
- android双进程守护
- 类加载器和代理
- Bitmap优化小结
- 1089. Insert or Merge (25)
- C#高级------字符串练习题
- 二十种实战调优MySQL性能优化的经验
- 查询优化 | MySQL慢查询优化
- 交通灯管理系统_编码思路及代码
- 使用指针遍历二维数组
- Service
- Innodb额外内存池的分配策略以及性能
- 0908Android基础四大组件之BroadcastReceiver
- 哎呦,不错哦
- 银行业务调度系统_编码思路及代码
- 黑马程序员-C语言-递归
- 聊聊编程语言学习之路