分享

RandomAccess接口理解

 liang1234_ 2018-05-23

根据javadoc上面的的解释是:

RandomAccess 是一个标记接口,用于标明实现该接口的List支持快速随机访问,主要目的是使算法能够在随机和顺序访问的list中表现的更加高效。

我们可以简单的看下Collections下的binarySearch方法的源码:

  1. public static <T>  
  2.     int binarySearch(List<? extends Comparable<? super T>> list, T key) {  
  3.         if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)  
  4.             return Collections.indexedBinarySearch(list, key);  
  5.         else  
  6.             return Collections.iteratorBinarySearch(list, key);  
  7.     }  


从源码中我们可以看到,在进行二分查找的时候,list会先判断是否是RandomAccess也即是否实现了RandomAccess接口,接着在调用想用的二分查找算法来进行,(其中: BINARYSEARCH_THRESHOLD Collections的一个常量(5000),它是二分查找的阀值。)如果实现了RandomAccess接口的List,执行indexedBinarySearch方法,否则执行 iteratorBinarySearch方法。

分别看下这两个方法的实现:


indexedBinarySearch 方法:

  1. private static <T>  
  2.     int indexedBinarySearch(List<? extends Comparable<? super T>> list, T key) {  
  3.         int low = 0;  
  4.         int high = list.size()-1;  
  5.   
  6.         while (low <= high) {  
  7.             int mid = (low + high) >>> 1;  
  8.             Comparable<? super T> midVal = list.get(mid);  
  9.             int cmp = midVal.compareTo(key);  
  10.   
  11.             if (cmp < 0)  
  12.                 low = mid + 1;  
  13.             else if (cmp > 0)  
  14.                 high = mid - 1;  
  15.             else  
  16.                 return mid; // key found  
  17.         }  
  18.         return -(low + 1);  // key not found  
  19.     }  

indexedBinarySearch 方法是直接通过get来访问元素


iteratorBinarySearch方法:

  1. private static <T>  
  2.     int iteratorBinarySearch(List<? extends Comparable<? super T>> list, T key)  
  3.     {  
  4.         int low = 0;  
  5.         int high = list.size()-1;  
  6.         ListIterator<? extends Comparable<? super T>> i = list.listIterator();  
  7.   
  8.         while (low <= high) {  
  9.             int mid = (low + high) >>> 1;  
  10.             Comparable<? super T> midVal = get(i, mid);  
  11.             int cmp = midVal.compareTo(key);  
  12.   
  13.             if (cmp < 0)  
  14.                 low = mid + 1;  
  15.             else if (cmp > 0)  
  16.                 high = mid - 1;  
  17.             else  
  18.                 return mid; // key found  
  19.         }  
  20.         return -(low + 1);  // key not found  
  21.     }  
iteratorBinarySearch中ListIterator来查找相应的元素


javadoc中特别指出:

It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a Listimplementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:

     for (int i=0, n=list.size(); i < n; i++)
         list.get(i);
 
runs faster than this loop:
     for (Iterator i=list.iterator(); i.hasNext(); )
         i.next();


总结:实现RandomAccess接口的的List可以通过简单的for循环来访问数据比使用iterator访问来的高效快速。

参考文档:https://docs.oracle.com/javase/7/docs/api/java/util/RandomAccess.html





    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多