keyset and entryset in java

http://www.importnew.com/26298.html

ImportNew
Home All Articles Information Web Architecture Basic Technology Books Tutorial Java Group Tool Resources
Java HashMap Traversal Mode Performance Discussion
2017/08/15 | Category: Basic Technology | 0 Comments | Tags : HASHMAP, traversal performance
Share to:
4Original source: Hosee
's implementation of HashMap will not be expanded here. For details, please refer to the implementation of HashMap in JDK7 and JDK8.

Before JDK8, you can use keySet or entrySet to traverse HashMap, and map was introduced in JDK8 .foreach to traverse.

Reason:

The keySet is actually traversed twice, one is to convert it to an Iterator object, and the other is to retrieve the value corresponding to the key from the hashMap. The entrySet only traverses once and puts both the key and value into the entry, which is more efficient. If it is JDK8, use the Map.foreach method.

1. keySet and entrySet

1.1 Basic usage

keySet:


Map map=new HashMap();
Iterator it=map.keySet().iterator();
Object key;
Object value;
while(it.hasNext()){
key=it. next();
value=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=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(); }
    }
In fact, it is already obvious here, when a value is to be obtained, keySet It is also necessary to get from HashMap. Compared with keySet, entrySet has less process of traversing the table, which is also the main difference in performance between the two.

2. Map.foreach

After JDK8, Map.foreach was introduced.

The essence of Map.foreach is still 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);
        }
    }
It is more convenient to use with lambda expressions.

2.1 Use Java8's foreach+lambda expression to traverse 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.
    if("E".equals(k)){
        System.out.println("Hello E");
    }
});

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989449&siteId=291194637