码迷,mamicode.com
首页 > 其他好文 > 详细

HashMap遍历时的性能对比

时间:2018-08-03 01:06:02      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:style   asm   keyset   性能对比   遍历   long   out   ati   stat   

 

使用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遍历时的性能对比

标签:style   asm   keyset   性能对比   遍历   long   out   ati   stat   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!