打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
RandomAccess接口理解

根据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





本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
为什么这些java接口没有抽象方法?
RandomAccess接口
快速排序算法的C语言实现
20190709 暑训 区间种类数
STL之next_permutation函数对各种类型的全排列实例
二分查找已排序数组
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服