【基础】RandomAccess

news/2024/7/3 11:52:21

  在List集合中,我们经常会用到ArrayList以及LinkedList集合,但是通过查看源码,就会发现ArrayList实现RandomAccess接口,但是RandomAccess接口里面是空的!Linked并没有实现RandomAccess接口。

 

RandomAccess接口是一个标志接口(Marker)

List集合实现这个接口,就能支持快速随机访问

Collections类中的binarySearch()方法,源码如下:

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);
    } 

 

indexedBinarySerach(list,key)源码:

    private static <T>
    int indexedBinarySearch(List<? extends Comparable<? super T>> list, T key) {
        int low = 0;
        int high = list.size()-1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            Comparable<? super T> midVal = list.get(mid);
            int cmp = midVal.compareTo(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
    }

 

iteratorBinarySerach(list,key)源码:

 

private static <T>
    int iteratorBinarySearch(List<? extends Comparable<? super T>> list, T key)
    {
        int low = 0;
        int high = list.size()-1;
        ListIterator<? extends Comparable<? super T>> i = list.listIterator();

        while (low <= high) {
            int mid = (low + high) >>> 1;
            Comparable<? super T> midVal = get(i, mid);
            int cmp = midVal.compareTo(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
    }

 

通过查看源代码,发现实现RandomAccess接口的List集合采用一般的for循环遍历,而未实现这接口则采用迭代器。

 

ArrayList用for循环遍历比iterator迭代器遍历快,LinkedList用iterator迭代器遍历比for循环遍历快,

所以说,当我们在做项目时,应该考虑到List集合的不同子类采用不同的遍历方式,能够提高性能!

然而有人发出疑问了,那怎么判断出接收的List子类是ArrayList还是LinkedList呢?

这时就需要用instanceof来判断List集合子类是否实现RandomAccess接口!

 

总结:RandomAccess接口这个空架子的存在,是为了能够更好地判断集合是否ArrayList或者LinkedList,从而能够更好选择更优的遍历方式,提高性能

转自:https://blog.csdn.net/weixin_39148512/article/details/79234817 

转载于:https://www.cnblogs.com/itplay/p/10600735.html


http://www.niftyadmin.cn/n/1494298.html

相关文章

oracle无法跟踪web发送的sql,如何通过跟踪客户端程序发出的sql的方法来优化SQL

简要说来&#xff0c;跟踪一个客户程序发出的SQL主要分成下面几步&#xff1a;1) 识别要跟踪的客户端程序到数据库的连接(后面都用session代替)&#xff0c;主要找出能唯一识别一个session的sid与serial#.2) 设定相应的参数&#xff0c;如打开时间开关(可以知道一个sql执行了多…

oracle的默认编码是,ORACLE 默认编码 GBK -UTF8编码

查看oracle数据库字符集&#xff1a;select userenv(language) from dual;SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL> startup mountORACLE instance started.Total System Global Area 135337420 bytesFixed Size …

nodemailer + express + h5 拖拽文件上传 实现发送邮件

一、部署 1、部署Express 2、准备一个邮箱并开始SMTP服务 二、服务器端 三、客户端 四、效果&#xff1a; 转载于:https://www.cnblogs.com/cwxwdm/p/10601646.html

下载github指定文件夹下的内容

傻瓜操作 http://downgit.zhoudaxiaa.com/#/home 窒息操作 sudo apt-get install subversiongithubdir #!/usr/bin/env pythonimport sys,osif __name__ __main__:urlsys.argv[1]durlurl.replace(tree/master,trunk)cmdf"svn checkout {durl}"os.system(cmd)./g…

dockerfile 的最佳实践

Dockerfile 编写nginx容器 [rootmast nginx]# cat Dockerfile FROM centos MAINTAINER zhaoruidong RUN yum -y install gcc gcc-c make openssl-devel pcre-devel gd-devel iproute net-tools telnet wget curl && yum clean all && rm -rf /var/cache/…

Java配合爬虫代理IP采集大众点评店铺信息

大众点评店铺网址格式如下&#xff1a;http://www.dianping.com/shop/6000000/http://www.dianping.com/shop/6000001/ shop后面的ID是连续的&#xff0c;范围是1-1500万&#xff0c;当然有许多店铺是不存在的(404错误)&#xff0c;实际的店铺数量在700万左右&#xff0c;这里是…

oracle oms启动慢,寻求帮助:oms起不来

先说下oem的结构&#xff0c;三个oms服务器(04&#xff0c;05&#xff0c;06)&#xff0c;repository database是个三节点的RAC(A&#xff0c;B&#xff0c;C)&#xff0c;现在04上的oms服务可以起来&#xff0c;oem也能用&#xff0c;但是05&#xff0c;06上的oms服务都起不来…

oracle 存储loop,oracle 写存储过程有返回值时 注意在loop循环处添加返回值:=

例子&#xff1a;create or replace procedure p_xl isv_count NUMBER(10);beginfor rs in(select yhbh from dbyh) loopv_count : osm_pkg_arc_limited_configs.F_LIMITED_METERS_CREATE(‘rs.yhbh‘,10001,---限量用水idsysdate,201706,0.00,10000);end loop;commit;end p_xl…