zoukankan      html  css  js  c++  java
  • java循环HashMap两种方法的效率比较

    一、循环HashMap的两种方式

    方式1:

    Iterator<Entry<String, String>> entryKeyIterator = entrySetMap.entrySet().iterator();
           while (entryKeyIterator.hasNext()) {
                Entry<String, String> e = entryKeyIterator.next();
                String value = e.getValue();
    }
    方式2:
    Iterator<String> keySetIterator = keySetMap.keySet().iterator();
            while (keySetIterator.hasNext()) {
            String key = keySetIterator.next();
            String value = keySetMap.get(key);
    }

    二、性能比较

    到底第一种方式的性能比第二种方式的性能高多少呢,通过一个简单的测试类可以看一下,测试代码如下

    public class Test {
    	public static void main(String[] args) {
    		HashMap<String, String> entrySetMap = new HashMap<String, String>();
    		HashMap<String, String> keySetMap = new HashMap<String, String>();
    		for (int i = 0; i < 500000; i++) {
    			entrySetMap.put("" + i, "entrySet");
    		}
    		for (int i = 0; i < 500000; i++) {
    			keySetMap.put("" + i, "keySet");
    		}
    		long startTimeOne = System.currentTimeMillis();
    		Iterator<Entry<String, String>> entryKeyIterator = entrySetMap.entrySet().iterator();
    		while (entryKeyIterator.hasNext()) {
    			Entry<String, String> e = entryKeyIterator.next();
    			//System.out.println(e.getValue());
    		}
    		StringBuffer result = new StringBuffer("entrySet spent times:");
    		result.append((System.currentTimeMillis() - startTimeOne));
    		System.out.println(result.toString());
    		
    		long startTimeTwo = System.currentTimeMillis();
    		Iterator<String> keySetIterator = keySetMap.keySet().iterator();
    		while (keySetIterator.hasNext()) {
    			String key = keySetIterator.next();
    			String value = keySetMap.get(key);
    			//System.out.println(value);
    		}
    		result.setLength(0);
    		result.append("keyset spent times:");
    		result.append((System.currentTimeMillis() - startTimeTwo));
    		System.out.println(result.toString());
    	}
    }

    通过测试发现,第一种方式的性能通常要比第二种方式高一倍

    三、原因分析

    通过查看源代码发现,调用这个方法keySetMap.keySet()会生成KeyIterator迭代器,其next方法只返回其key值

    private class KeyIterator extends HashIterator<K> {   
    	public K next() {
    		return nextEntry().getKey();
    	}
    }   
    private class KeyIterator extends HashIterator<K> {
    	public K next() {
    		return nextEntry().getKey();
    	}
    }

    而调用entrySetMap.entrySet()方法会生成EntryIterator 迭代器,其next方法返回一个Entry对象的一个实例,其中包含key和value

    private class EntryIterator extends HashIterator<Map.Entry<K,V>> {   
    	public Map.Entry<K,V> next() {
    		return nextEntry();
    	}
    }
    private class EntryIterator extends HashIterator<Map.Entry<K,V>> {
    	public Map.Entry<K,V> next() {
    		return nextEntry();
    	}
    }

    二者在此时的性能应该是相同的,但方式一再取得key所对应的value时,此时还要访问Map的这个方法,这时,方式一多遍历了一次table

    public V get(Object key) {
    	Object k = maskNull(key);
    	int hash = hash(k);
    	int i = indexFor(hash, table.length);
    	Entry<K,V> e = table[i]; 
    	while (true) {
    		if (e == null)
    			return null;
    		if (e.hash == hash && eq(k, e.key)) 
    			return e.value;
    		e = e.next;
    	}
    }
    public V get(Object key) {
    	Object k = maskNull(key);
    	int hash = hash(k);
    	int i = indexFor(hash, table.length);
    	Entry<K,V> e = table[i];
    	while (true) {
    		if (e == null)
    			return null;
    		if (e.hash == hash && eq(k, e.key))
    			return e.value;
    		e = e.next;
    	}
    }

    这个方法就是二者性能差别的主要原因.

  • 相关阅读:
    用Inno Setup来解决.NetFramework安装问题
    NSIS安装制作基础教程[初级篇], 献给对NSIS有兴趣的初学者
    NSIS
    Nullsoft教程 NSIS初学者图文教程一
    flex 实例Demo
    计算机组成原理
    Spring知识点提炼
    JAVA Web从前端到后台常用框架介绍
    使用navicat将mysql转换成sqlserver
    JDK 5 ~ 10 新特性倾情整理!
  • 原文地址:https://www.cnblogs.com/brucetie/p/3569679.html
Copyright © 2011-2022 走看看