第一种:通过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
版权声明:本文为博主原创文章,转载请附上博文链接!