1.Conllection.BinarySearch(List,key,c),參數有比較器,源碼如下
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) {
if (c==null)//判斷是否有比較器
return binarySearch((List<? extends Comparable<? super T>>) list, key);
if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
return Collections.indexedBinarySearch(list, key, c);
else
return Collections.iteratorBinarySearch(list, key, c);
}
可以看到,第一個if判斷傳入的參數是否有比較器,如果沒有就會調用Conllection.BinarySearch(List,key);
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);
}
在第二個if中首先判斷list是否實現RandomAccess接口(RandomAccess接口這個空架子的存在,是爲了能夠更好地判斷集合是否ArrayList或者LinkedList,從而能夠更好選擇更優的遍歷方式,提高性能!,instanceof其作用是用來判斷某對象是否爲某個類)或接口類型或者list的size有沒有超過BINARYSEARCH_THRESHOLD(這個是常數5000),判斷通過,考慮效率問題實現RandomAccess接口的List集合採用一般的for循環遍歷,而未實現這接口則採用迭代器。就會執行Collections.indexedBinarySearch(list, key, c);
private static <T> int indexedBinarySearch(List<? extends T> l, T key, Comparator<? super T> c) {
int low = 0;
int high = l.size()-1;
while (low <= high) {
int mid = (low + high) >>> 1;
T midVal = l.get(mid);
int cmp = c.compare(midVal, key);
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found
}
這個裏邊的>>>是一個無符號的右移操作,有符號操作符>>也是右移操作,這裏計算平均值使用>>>取代>>,恐怕是因爲可能出現很大的數字,這些數字單獨用不會超過Integer.MAX_VALUE,但求和之後可能超過,這時如果使用>>或者/來計算,會因爲溢出而算出負數結果。
若是list是否實現RandomAccess接口,就會執行Collections.iteratorBinarySearch(list, key, c),通過使用迭代器來提高性能;
private static <T> int iteratorBinarySearch(List<? extends T> l, T key, Comparator<? super T> c) {
int low = 0;
int high = l.size()-1;
ListIterator<? extends T> i = l.listIterator();
while (low <= high) {
int mid = (low + high) >>> 1;
T midVal = get(i, mid);
int cmp = c.compare(midVal, key);
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found
}
/**
* Gets the ith element from the given list by repositioning the specified
* list listIterator.
*/
private static <T> T get(ListIterator<? extends T> i, int index) {
T obj = null;
int pos = i.nextIndex();
if (pos <= index) {
do {
obj = i.next();
} while (pos++ < index);
} else {
do {
obj = i.previous();
} while (--pos > index);
}
return obj;
}
借鑑:
對RandomAccess接口的解釋https://blog.csdn.net/weixin_39148512/article/details/79234817
>>>和>>操作符的區別https://www.cnblogs.com/zt007/p/7169735.html?utm_source=itdadao&utm_medium=referral