日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HashMap的四种访问方式

發布時間:2023/12/13 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HashMap的四种访问方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一種:通過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 }

?


結果:


作者:ceoicac
來源:CSDN
原文:https://blog.csdn.net/ceoicac/article/details/77113068
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

轉載于:https://www.cnblogs.com/KeepDoingSomething/p/9904140.html

總結

以上是生活随笔為你收集整理的HashMap的四种访问方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。