list遍历效率

  1. package com.zbalpha.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6.   
  7. public class ListTest {  
  8.     public static void main(String args[]){  
  9.         List<Long> lists = new ArrayList<Long>();  
  10.   
  11.         for(Long i=0l;i<1000000l;i++){  
  12.             lists.add(i);  
  13.         }    
  14.           
  15.         Long oneOk = oneMethod(lists);  
  16.         Long twoOk = twoMethod(lists);  
  17.         Long threeOk = threeMethod(lists);  
  18.         Long fourOk = fourMethod(lists);  
  19.           
  20.         System.out.println("One:" + oneOk);  
  21.         System.out.println("Two:" + twoOk);  
  22.         System.out.println("Three:" + threeOk);  
  23.         System.out.println("four:" + fourOk);  
  24.           
  25.     }  
  26.       
  27.     public static Long oneMethod(List<Long> lists){  
  28.           
  29.         Long timeStart = System.currentTimeMillis();  
  30.         for(int i=0;i<lists.size();i++)    {  
  31.             System.out.println(lists.get(i));  
  32.         }  
  33.         Long timeStop = System.currentTimeMillis();  
  34.   
  35.         return timeStop -timeStart ;  
  36.     }  
  37.       
  38.     public static Long twoMethod(List<Long> lists){  
  39.           
  40.         Long timeStart = System.currentTimeMillis();  
  41.         for(Long string : lists)    {  
  42.             System.out.println(string);  
  43.         }  
  44.         Long timeStop = System.currentTimeMillis();  
  45.   
  46.         return timeStop -timeStart ;  
  47.     }  
  48.       
  49.     public static Long threeMethod(List<Long> lists){  
  50.           
  51.         Long timeStart = System.currentTimeMillis();  
  52.         Iterator<Long> it = lists.iterator();  
  53.         while (it.hasNext())  
  54.         {  
  55.                 System.out.println(it.next());  
  56.         }  
  57.         Long timeStop = System.currentTimeMillis();  
  58.   
  59.         return timeStop -timeStart ;  
  60.     }      
  61.       
  62.           
  63.       
  64.     public static Long fourMethod(List<Long> lists){  
  65.           
  66.         Long timeStart = System.currentTimeMillis();  
  67.         for(Iterator<Long> i = lists.iterator(); i.hasNext();)    {  
  68.             System.out.println(i.next());  
  69.         }  
  70.         Long timeStop = System.currentTimeMillis();  
  71.   
  72.         return timeStop -timeStart ;  
  73.     }      


One:14109
Two:14000
Three:15141
four:14297

原文地址:https://www.cnblogs.com/QAZLIU/p/5952115.html