0
点赞
收藏
分享

微信扫一扫

java RandomAccess 遍历效率


 RandomAccess 是判断集合是否支持快速随即访问,以下是个测试用例:

JDK中推荐的是对List集合尽量要实现RandomAccess接口

如果集合类是RandomAccess的实现,则尽量用for(int i = 0; i < size; i++) 来遍历而不要用Iterator迭代器来遍历,在效率上要差一些。反过来,如果List是Sequence List,则最好用迭代器来进行迭代。


JDK中说的很清楚,在对List特别是Huge size的List的遍历算法中,要尽量来判断是属于RandomAccess(如ArrayList)还是Sequence List (如LinkedList),因为适合RandomAccess List的遍历算法,用在Sequence List上就差别很大,常用的作法就是:
    要作一个判断:
  

验证:

if (list instance of RandomAccess) {
        for(int m = 0; m < list.size(); m++){}
    }else{
        Iterator iter = list.iterator();
        while(iter.hasNext()){}
    }


Java代码   

java RandomAccess 遍历效率_java


    1. <span style="font-size: small;">/*
    2.  * To change this template, choose Tools | Templates
    3.  * and open the template in the editor.
    4.  */
    5. package
    6.   
    7. import
    8. import
    9. import
    10. import
    11. import
    12.   
    13. /**
    14.  *
    15.  * @author bolong
    16.  */
    17. public class
    18. // 初始化列表
    19.   
    20. public static void initList(List list, int
    21. for (int i = 0; i < n; i++) {  
    22.             list.add(i);  
    23.         }  
    24.     }  
    25. //使用循环进行对列表的迭代
    26.   
    27. public static void
    28. long starttime = 0;  
    29. long endtime = 0;  
    30.         starttime = System.currentTimeMillis();  
    31. for (int count = 0; count <= 1000; count++) {  
    32. for (int i = 0; i < list.size(); i++) {  
    33.                 list.get(i);  
    34.             }  
    35.         }  
    36.         endtime = System.currentTimeMillis();  
    37. "使用loop迭代一共花了" + (endtime - starttime) + "ms时间");  
    38.   
    39.     }  
    40. //使用迭代器对列表进行迭代
    41.   
    42. public static void
    43. long starttime = 0;  
    44. long endtime = 0;  
    45.         starttime = System.currentTimeMillis();  
    46. for (int count = 0; count <= 1000; count++) {  
    47. for
    48.                 itr.next();  
    49.             }  
    50.         }  
    51.         endtime = System.currentTimeMillis();  
    52. "使用Iterator迭代一共花了" + (endtime - starttime) + "ms时间");  
    53.     }  
    54.   
    55. public static void
    56.   
    57. long starttime = 0;  
    58. long endtime = 0;  
    59. if (list instanceof
    60. "该list实现了RandomAccess接口");  
    61.             starttime = System.currentTimeMillis();  
    62. for (int count = 0; count <= 1000; count++) {  
    63. for (int i = 0; i < list.size(); i++) {  
    64.                     list.get(i);  
    65.                 }  
    66.             }  
    67.             endtime = System.currentTimeMillis();  
    68. "迭代一共花了" + (endtime - starttime) + "ms时间");  
    69. else
    70. "该list未实现RandomAccess接口");  
    71.             starttime = System.currentTimeMillis();  
    72. for (int count = 0; count <= 1000; count++) {  
    73. for
    74.                     itr.next();  
    75.                 }  
    76.             }  
    77.             endtime = System.currentTimeMillis();  
    78. "迭代一共花了" + (endtime - starttime) + "ms时间");  
    79.         }  
    80.     }  
    81.   
    82. public static void
    83. new
    84. new
    85. 1000);  
    86. 1000);  
    87.         traverse(arraylist);  
    88.         traverse(linkedlist);  
    89.         traverseWithIterator(arraylist);  
    90.         traverseWithLoop(arraylist);  
    91.         traverseWithIterator(linkedlist);  
    92.         traverseWithLoop(linkedlist);  
    93.     }  
    94. }  
    95. </span>


     运行程序输出的结果为:


    该list实现了RandomAccess接口
    迭代一共花了47ms时间
    该list未实现RandomAccess接口
    迭代一共花了15ms时间
    使用Iterator迭代一共花了79ms时间
    使用loop迭代一共花了46ms时间
    使用Iterator迭代一共花了47ms时间
    使用loop迭代一共花了797ms时间


    结论:

    根据程序输出的结果的确证明了,arraylist等实现了RandomAccessj接口的类在进行迭代时使用loop效率更高,而linkedList那些未实现该接口的类在进行迭代时使用Iterator进行迭代效率更高.

    举报

    相关推荐

    0 条评论