生活随笔
收集整理的這篇文章主要介紹了
HashMap的四种访问方式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
第一種:通過Map.entrySet使用iterator遍歷key和value
?
1 public void visit_1(HashMap<String,Integer>
hm){
2 Iterator<Map.Entry<String,Integer>> it =
hm.entrySet().iterator();
3 while(it.hasNext()){
4 Map.Entry<String ,Integer> entry =
it.next();
5 String key =
entry.getKey();
6 Integer value =
entry.getValue();
7 }
8 }
?
第二種:通過Key來遍歷value
1 public void visit_2(HashMap<String,Integer>
hm){
2 for (String key:hm.keySet()){
3 Integer value =
hm.get(key);
4 }
5 }
?
第三種:通過Map.Entry遍歷key和value
1 public void visit_3(HashMap<String,Integer>
hm){
2 for(Map.Entry<String,Integer>
entry:hm.entrySet()){
3 String key =
entry.getKey();
4 Integer value =
entry.getValue();
5 }
6 }
?
第四種:通過Map.keySet使用iterator遍歷key和value
1 public void visit_4(HashMap<Integer,String>
hm){
2 long startTime =
System.currentTimeMillis();
3 Iterator<Integer> it =
hm.keySet().iterator();
4 while(it.hasNext()){
5 Integer key =
it.next();
6 String value =
hm.get(key);
7 }
8 System.out.println("visit_4 10000000 entry:"
9 + (System.currentTimeMillis()-startTime) + " milli seconds"
);
10 }
?
四種方法比較:
1 package chapter08.c86.c862;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 /**
8 * Created by ceoicac on 2017/8/12 10:22.
9 */
10 public class HashMapVisitTest {
11 public void visit_1(HashMap<Integer,String>
hm){
12 long startTime =
System.currentTimeMillis();
13 Iterator<Map.Entry<Integer,String>> it =
hm.entrySet().iterator();
14 while(it.hasNext()){
15 Map.Entry<Integer,String> entry =
it.next();
16 Integer key =
entry.getKey();
17 String value =
entry.getValue();
18 }
19 System.out.println("visit_1 10000000 entry:"
20 + (System.currentTimeMillis()-startTime) + " milli seconds"
);
21 }
22 public void visit_2(HashMap<Integer,String>
hm){
23 long startTime =
System.currentTimeMillis();
24 for (Integer key:hm.keySet()){
25 String value =
hm.get(key);
26 }
27 System.out.println("visit_1 10000000 entry:"
28 + (System.currentTimeMillis()-startTime) + " milli seconds"
);
29 }
30 public void visit_3(HashMap<Integer,String>
hm){
31 long startTime =
System.currentTimeMillis();
32 for(Map.Entry<Integer,String>
entry : hm.entrySet()){
33 Integer key =
entry.getKey();
34 String value =
entry.getValue();
35 }
36 System.out.println("visit_1 10000000 entry:"
37 + (System.currentTimeMillis()-startTime) + " milli seconds"
);
38 }
39 public void visit_4(HashMap<Integer,String>
hm){
40 long startTime =
System.currentTimeMillis();
41 Iterator<Integer> it =
hm.keySet().iterator();
42 while(it.hasNext()){
43 Integer key =
it.next();
44 String value =
hm.get(key);
45 }
46 System.out.println("visit_1 10000000 entry:"
47 + (System.currentTimeMillis()-startTime) + " milli seconds"
);
48 }
49 public static void main(String [] args){
50 HashMap<Integer,String> hm =
new HashMap<>
();
51 for(
int i = 1;i <= 10000000;++
i){
52 hm.put(i,"num: " +
i);
53 }
54 new HashMapVisitTest().visit_1(hm);
55 new HashMapVisitTest().visit_2(hm);
56 new HashMapVisitTest().visit_3(hm);
57 new HashMapVisitTest().visit_4(hm);
58
59 }
60 }
?
結(jié)果:
作者:ceoicac
來源:CSDN
原文:https://blog.csdn.net/ceoicac/article/details/77113068
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!
轉(zhuǎn)載于:https://www.cnblogs.com/KeepDoingSomething/p/9904140.html
總結(jié)
以上是生活随笔為你收集整理的HashMap的四种访问方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。