Java 的Conllection.BinarySearch

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章