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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值