热门

最新

红包

立Flag

投票

同城

我的

发布
weixin_46639323
春风万里不如你
3 年前
trueweixin_46639323

RandomAccess 接口
public interface RandomAccess {
}
public static <T>
int binarySearch(List<? extends Comparable<? super T>> list, T key) {
if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
return Collections.indexedBinarySearch(list, key);
else
return Collections.iteratorBinarySearch(list, key);
}

RandomAccess接口这个空架子的存在,是为了能够更好地判断传入的集合list是ArrayList还是LinkedList,indexedBinarySearch方法是使用for循环,而iteratorBinarySearch方法使用的是迭代器;ArrayList使用for循环性能更高,LinkedList使用迭代器的性能更高。
执行不同方法搜索,从而能够更好选择更优的遍历方式,提高性能!

CSDN App 扫码分享
分享
评论
点赞
打赏
  • 复制链接
  • 举报
下一条:
1
立即登录