keySet 与entrySet 遍历HashMap性能差别

转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51669534 文章出自:薛瑄的博客

你也可以查看我的其他同类文章,也会让你有一定的收货!

一、常用的遍历HashMap的两种方法

第一种: entrySet()

Map map = new HashMap(); 
Iterator iter = map.entrySet().iterator(); 
while (iter.hasNext()) { 
    Map.Entry entry = (Map.Entry) iter.next(); 
    Object key = entry.getKey(); 
    Object val = entry.getValue(); 
} 

效率高,以后一定要使用此种方式!

第二种: keySet()

Map map = new HashMap(); 
Iterator iter = map.keySet().iterator(); 
while (iter.hasNext()) { 
    Object key = iter.next(); 
    Object val = map.get(key); 
} 

效率低,以后尽量少使用!

二、性能比较

public class HashMapTest {  
    public static void main(String[] args) {  

        HashMap<String, String> keySetMap = new HashMap<String, String>();  
        HashMap<String, String> entrySetMap = new HashMap<String, String>();  

        for (int i = 0; i < 1000; i++) {  
            keySetMap.put("" + i, "keySet");  
        }  
        for (int i = 0; i < 1000; i++) {  
            entrySetMap.put("" + i, "entrySet");  
        }  

        long startTimeOne = System.currentTimeMillis();  
        Iterator<String> keySetIterator = keySetMap.keySet().iterator();  
        while (keySetIterator.hasNext()) {  
            String key = keySetIterator.next();  
            String value = keySetMap.get(key);  
            System.out.println(value);  
        }  

        System.out.println("keyset spent times:"  
                + (System.currentTimeMillis() - startTimeOne));  

        long startTimeTwo = System.currentTimeMillis();  

        Iterator<Entry<String, String>> entryKeyIterator = entrySetMap  
                .entrySet().iterator();  
        while (entryKeyIterator.hasNext()) {  
            Entry<String, String> e = entryKeyIterator.next();  
            System.out.println(e.getValue());  
        }  
        System.out.println("entrySet spent times:"  
                + (System.currentTimeMillis() - startTimeTwo));  

    }  
}  

测试结果:

keyset 
.
.
keyset spent times:20
entrySet
.
.
entrySet spent times:7

我的测试结果entrySet ()比keyset()效率高很多,有的测试1000组数据,可能看不到太大差异,可能和cpu性能有关。

三.原因分析:

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

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();  
       }  
 }

二者在此时的性能应该是相同的,但方式一再取得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;  
        }  
    }

转载:http://kim-miao.iteye.com/blog/736143
http://blog.csdn.net/xueyepiaoling/article/details/5217709

发布了244 篇原创文章 · 获赞 799 · 访问量 234万+
展开阅读全文

KeySetentrySet为什么会有两种不同的结果。。

01-14

import java.util.*; class Student implements Comparable<Student> { private String id; private String name; Student(String id,String name) { this.id = id; this.name = name; } public String getId() { return id; } public String getName() { return name; } public String toString() { return id +"::" + name; } public int hashCode() { return this.id.hashCode() + this.name.hashCode()*30; } public boolean equals(Object obj) { if(obj instanceof Student) throw new ClassCastException("类型不符合"); Student s = (Student)obj; return this.name.equals(s.name) && this.id.equals(s.id); } public int compareTo(Student s) { if(id.compareTo(s.id)==0) return this.name.compareTo(s.name); return this.id.compareTo(s.id); } } class MyCmpt implements Comparator<String> { public int compare(String o1,String o2) { return 1; } } class MapDemo3 { public static void demo() { TreeMap<String,List<Student>> czbk = new TreeMap<String,List<Student>>(new MyCmpt()); List<Student> yure = new ArrayList<Student>(); List<Student> jiuye = new ArrayList<Student>(); czbk.put("yure",yure); czbk.put("jiuye",jiuye); yure.add(new Student("01","zhangsan")); yure.add(new Student("02","lisisan")); jiuye.add(new Student("01","zhangsansan")); jiuye.add(new Student("02","zhangxiaosan")); Set<Map.Entry<String,List<Student>>> e = czbk.entrySet(); for(Iterator<Map.Entry<String,List<Student>>> it = e.iterator(); it.hasNext();) { Map.Entry<String,List<Student>> me = it.next(); String st = me.getKey(); List<Student> stu = me.getValue(); System.out.println(st); getInfos(stu); } /*Set<String> s = czbk.keySet(); for(Iterator<String> it = s.iterator();it.hasNext();) { String str = it.next(); List<Student> stu = czbk.get(str); System.out.println(str); System.out.println(stu); }*/ } public static void getInfos(List<Student> list) { for(Iterator<Student> it = list.iterator();it.hasNext();) { Student s = it.next(); System.out.println(s); } } public static void main(String[] args) { demo(); } } 上面的代码我加入了比较器之后,用entrySet方法用迭代器去遍历键还是指向了值,但是我用keySet方法去遍历,键却指向了空,求解惑? 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览