最代码广告位
tianwc的gravatar头像
tianwc2015-09-29 09:18:06

java HashMap四种遍历查询方式耗时性能对比

/**
 * @param args
 */
public static void main(String[] args) {
	// TODO Auto-generated method stub
	Map<String, String> map = new TreeMap<String, String>();
	int num = 1000000;
	String key, value;
	// 存放100万条数据
	for (int i = 1; i <= num; i++) {
		key = "" + i;
		value = "value";
		map.put(key, value);

	}
	// -------------------遍历 方法时间比较--------------------
	long s = System.currentTimeMillis();
	Iterator<String> iter1 = map.keySet().iterator();
	while (iter1.hasNext()) {
		key = iter1.next();
		value = map.get(key);
	}
	// 测试时间:200
	for (String key1 : map.keySet()) {
		value = map.get(key1);
	}
	// 测试时间:219
	Iterator<Entry<String, String>> iter2 = map.entrySet().iterator();
	Entry<String, String> entry;
	while (iter2.hasNext()) {
		entry = iter2.next();
		key = entry.getKey();
		value = entry.getValue();
	}
	// 测试时间:41
	for (Entry<String, String> entry1 : map.entrySet()) {
		key = entry1.getKey();
		value = entry1.getValue();
	}
	// 测试时间:40
	long e = System.currentTimeMillis();
	System.out.println("测试时间:" + (e - s));

}

打赏

顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友