首页 > 其他 > 详细

HashMap遍历时的性能对比

时间:2018-08-02 23:45:47      阅读:158      评论:0      收藏:0      [点我收藏+]

 

使用KeySet和EntrySet遍历的差别

 1 public static void main(String[] args) {
 2 
 3         HashMap<Integer, Integer> hasMap = new HashMap();
 4         for (int i = 0; i < 10000; i++) {
 5             hasMap.put(i, i);
 6         }
 7 
 8         Long j = System.currentTimeMillis();
 9         for (int i = 1; i < 100000; i++) {
10             for (Map.Entry<Integer, Integer> integerIntegerEntry : hasMap.entrySet()) {
11                 integerIntegerEntry.getKey();
12                 integerIntegerEntry.getValue();
13             }
14         }
15         System.out.println("使用EntrySet:" + (System.currentTimeMillis() - j));
16 
17         Long k = System.currentTimeMillis();
18         for (int i = 1; i < 100000; i++) {
19             for (Integer key : hasMap.keySet()) {
20                 hasMap.get(key);
21             }
22         }
23         System.out.println("使用KeySet:" + (System.currentTimeMillis() - k));
24 
25     }

运行多次后,两者差别有2秒左右

 

结论:使用EntrySet遍历时性能更高

原因分析:。。。

 

HashMap遍历时的性能对比

原文:https://www.cnblogs.com/yszzu/p/9410741.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!