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 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值