HashMap中的几种遍历方式对比

 1 import java.util.HashMap;
 2 import java.util.Iterator;
 3 import java.util.Map;
 4 
 5 /**
 6  * @Author:KoVaVo
 7  * @Version:1.0.0
 8  * @Description:
 9  */
10 public class HashMapTest {
11     public static void main(String[] args) {
12         //hashMap的几种便利方式
13         HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
14         hashMap.put(1,"1");
15         hashMap.put(2,"2");
16         hashMap.put(3,"3");
17         System.out.println("=====keyset=====");
18         long l = System.currentTimeMillis();
19         for (int key : hashMap.keySet()) {
20             System.out.println(key+"..."+hashMap.get(key));
21         }
22         System.out.println(l-System.currentTimeMillis());
23         System.out.println("=====iterator=====");
24         l=System.currentTimeMillis();
25         Iterator<Map.Entry<Integer, String>> iterator = hashMap.entrySet().iterator();
26         while (iterator.hasNext()){
27             Map.Entry<Integer,String> next = iterator.next();
28             System.out.println(next.getValue()+"...iterator");
29         }
30         System.out.println(l-System.currentTimeMillis());
31         System.out.println("=====entryset=====");
32         l=System.currentTimeMillis();
33         for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
34             System.out.println(entry.getValue());
35         }
36         System.out.println(l-System.currentTimeMillis());
37     }
38 }

 

posted @ 2020-04-11 12:55  白天黑天阴天  阅读(274)  评论(0编辑  收藏  举报