复习了下集合,在网上看了好多有关集合的例子和概念,发现对于hashM和hashtable的速度谁快的问题有不同的答案,只能自己验证一下了,下面是我写的一个测试例子;
运行上面例子得到结果为:
事实证明还是hashMap更快
package collection.deng;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
public class collection {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date tab1 = new Date();
System.out.println("Hasetable测试开始时间" + format.format(tab1));
addHashtable();
Date tab2 = new Date();
System.out.println("Hasetable测试结束时间" + format.format(tab2));
System.out.println("用时" +(tab2.getTime()-tab1.getTime()));
Date map1 = new Date();
System.out.println("HashMap测试开始时间" + format.format(map1));
addHashMap();
Date map2 = new Date();
System.out.println("HashMap测试结束时间" + format.format(map2));
System.out.println("用时" +(map2.getTime()-map1.getTime()));
}
public static void addHashtable(){
Hashtable<String, String> table = new Hashtable<String, String>();
for(int i=0;i<10000;i++){
table.put(i+"key", i+"value");
}
}
public static void addHashMap(){
HashMap<String, String> map = new HashMap<String, String>();
for(int i=0;i<10000;i++){
map.put(i+"key", i+"value");
}
}
}
运行上面例子得到结果为:
Hasetable测试开始时间2016-08-06 21:05:11
Hasetable测试结束时间2016-08-06 21:05:11
用时32
HashMap测试开始时间2016-08-06 21:05:11
HashMap测试结束时间2016-08-06 21:05:11
用时15
事实证明还是hashMap更快