Java HashMap遍歷方式性能探討

分類:IT技術 時間:2017-08-07

關於HashMap的實現這裏就不展開了,具體可以參考JDK7與JDK8中HashMap的實現

JDK8之前,可以使用keySet或者entrySet來遍歷HashMap,JDK8中引入了map.foreach來進行遍歷。

原因:

keySet其實是遍歷了2次,一次是轉為Iterator對象,另一次是從hashMap中取出key所對應的value。而entrySet只是遍歷了一次就把key和value都放到了entry中,效率更高。如果是JDK8,使用Map.foreach方法。 

1. keySet和entrySet

1.1 基本用法

keySet:

Map map=new HashMap();
Iterator it=map.keySet().iterator();
Object key;
Object value;
while(it.hasNext()){
key=it.next();
value=https://my.oschina.net/hosee/blog/map.get(key);
system.out.println(key+":"+value);
}

entrySet:

Map map=new HashMap();
Iterator it=map.entrySet().iterator();
Object key;
Object value;
while(it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
key=entry.getKey();
value=https://my.oschina.net/hosee/blog/entry.getValue();
System.out.println(key+"="+value);
}

源碼上看:

keySet:

final class KeyIterator extends HashIterator
        implements Iterator<K> {
        public final K next() { return nextNode().key; }
    }

entrySet:

final class EntryIterator extends HashIterator
        implements Iterator<Map.Entry<K,V>> {
        public final Map.Entry<K,V> next() { return nextNode(); }
    }

其實這裏已經很明顯了,當要得到某個value時,keySet還需要從HashMap中get,entrySet相比keySet少了遍歷table的過程,這也是兩者性能上的主要差別。

2. Map.foreach

在JDK8以後,引入了Map.foreach。

Map.foreach本質仍然是entrySet

default void forEach(BiConsumer<? super K, ? super V> action) {
        Objects.requireNonNull(action);
        for (Map.Entry<K, V> entry : entrySet()) {
            K k;
            V v;
            try {
                k = entry.getKey();
                v = entry.getValue();
            } catch(IllegalStateException ise) {
                // this usually means the entry is no longer in the map.
                throw new ConcurrentModificationException(ise);
            }
            action.accept(k, v);
        }
    }

配合lambda表達式一起使用,操作起來更加方便。

2.1 使用Java8的foreach+lambda表達式遍歷Map

Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);

items.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v));

items.forEach((k,v)->{
    System.out.println("Item : " + k + " Count : " + v);
    if("E".equals(k)){
        System.out.println("Hello E");
    }
});

 

Reference:

1. http://lizhuang.iteye.com/blog/2356044

2. http://blog.163.com/fw_long/blog/static/51771186201392982041337/

3. https://yq.aliyun.com/articles/44712


Tags: 遍歷 value entrySet HashMap key keySet

文章來源:


ads
ads

相關文章
ads

相關文章

ad