开发学院WEB开发Jsp Java中遍历大容量map的正确方法 阅读

Java中遍历大容量map的正确方法

 2010-10-21 04:35:53 来源:WEB开发网   
核心提示:第四种比较少用,因为我们大多数时候都是同时需要key和value的综上所述,Java中遍历大容量map的正确方法(2),如果map里面内容比较少,其实采用哪种方式都可以,则无此问题,对这个有兴趣的同学,第一种和第三种相对简洁一些;但是一旦容量非常大时,更推荐采用第三种方式

第四种比较少用,因为我们大多数时候都是同时需要key和value的

综上所述,如果map里面内容比较少,其实采用哪种方式都可以,第一种和第三种相对简洁一些;但是一旦容量非常大时,更推荐采用第三种方式,相比于第一种将极大地节省性能。

修改一下代码,对执行时间执行一下测试:

import java.util.HashMap;
import java.util.Map;

public class MapTest {
	static long MAX_LONG = 1000000L;
	static int TIMES = 10;
	static Map<String, String> map1 = new HashMap<String, String>();
	static Map<String, String> map2 = new HashMap<String, String>();
	static {
		for (long i = 0; i < MAX_LONG; i++) {
			map1.put("1" + i, "abc" + i);
			map2.put("2" + i, "def" + i);
		}
	}

	public static void main(String[] args) {

		String valueStr = ""; 
		String keyStr = "";
		long start, end;
		double totalMs;



		totalMs = 0;
		for (int i = 0; i < TIMES; i++) {

			// 通过Map.keySet遍历key和value
			start = System.currentTimeMillis();
			for (String key : map1.keySet()) {
				keyStr = key;
				valueStr = map1.get(key);
			}
			end = System.currentTimeMillis();
			System.out.println("通过Map.keySet遍历key和value耗时 " + (end - start)
					+ " ms ");
			totalMs += (end - start);
		}
		System.out.println("Times : " + TIMES + ", totalMs : " + totalMs
				+ "ms, average :" + totalMs / TIMES + "ms");
		
		
		totalMs = 0;
		for (int i = 0; i < TIMES; i++) {
			// 通过Map.entrySet遍历key和value
			start = System.currentTimeMillis();
			for (Map.Entry<String, String> entry : map2.entrySet()) {
				keyStr = entry.getKey();
				valueStr = entry.getValue();
			}
			end = System.currentTimeMillis();
			System.out.println("通过Map.entrySet遍历key和value耗时 " + (end - start)
					+ " ms ");
			totalMs += (end - start);
		}
		System.out.println("Times : " + TIMES + ", totalMs : " + totalMs
				+ "ms, average :" + totalMs / TIMES + "ms");

	}

}

以下是测试结果:

通过Map.keySet遍历key和value耗时 186 ms 
通过Map.keySet遍历key和value耗时 189 ms 
通过Map.keySet遍历key和value耗时 87 ms 
通过Map.keySet遍历key和value耗时 89 ms 
通过Map.keySet遍历key和value耗时 84 ms 
通过Map.keySet遍历key和value耗时 92 ms 
通过Map.keySet遍历key和value耗时 85 ms 
通过Map.keySet遍历key和value耗时 94 ms 
通过Map.keySet遍历key和value耗时 89 ms 
通过Map.keySet遍历key和value耗时 91 ms 
Times : 10, totalMs : 1086.0ms, average :108.6ms
通过Map.entrySet遍历key和value耗时 112 ms 
通过Map.entrySet遍历key和value耗时 98 ms 
通过Map.entrySet遍历key和value耗时 71 ms 
通过Map.entrySet遍历key和value耗时 65 ms 
通过Map.entrySet遍历key和value耗时 65 ms 
通过Map.entrySet遍历key和value耗时 64 ms 
通过Map.entrySet遍历key和value耗时 64 ms 
通过Map.entrySet遍历key和value耗时 65 ms 
通过Map.entrySet遍历key和value耗时 65 ms 
通过Map.entrySet遍历key和value耗时 65 ms 
Times : 10, totalMs : 734.0ms, average :73.4ms

可以看出,百万级别的量时,使用keySet和entrySet遍历,执行时间大概为1.5:1,这并不是最主要的差异。真正的差异还是必须看代码:

map.get(key))

时执行的hash操作

/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*
* <p>More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
* key.equals(k))}, then this method returns {@code v}; otherwise
* it returns {@code null}.  (There can be at most one such mapping.)
*
* <p>A return value of {@code null} does not <i>necessarily</i>
* indicate that the map contains no mapping for the key; it's also
* possible that the map explicitly maps the key to {@code null}.
* The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*
* @see #put(Object, Object)
*/
public V get(Object key) {
if (key == null)
    return getForNullKey();
int hash = hash(key.hashCode());
for (Entry<K,V> e = table[indexFor(hash, table.length)];
     e != null;
     e = e.next) {
    Object k;
    if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
	return e.value;
}
return null;
}
    

无奇不有地

计算hashCode是CPU密集运算,非常耗费CPU资源,如果对一个比较大的map进行遍历,会出现CPU迅速飚高的现象,直接影响机器的响应速度,在多线程的情况下,简直就是一场灾难,而采用entrySet来进行遍历,则无此问题,对这个有兴趣的同学,可以使用自己的机器试一试。

上一页  1 2 

Tags:遍历 map

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接