hashMap和hashtable的速度比较

复习了下集合,在网上看了好多有关集合的例子和概念,发现对于hashM和hashtable的速度谁快的问题有不同的答案,只能自己验证一下了,下面是我写的一个测试例子;


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更快

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值