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; } }
这个方法就是二者性能差别的主要原因.