当前位置: 首页 > news >正文

RandomAccess接口

  jdk中有个RandomAccess接口,这是一个标记接口(Marker),它没有任何方法,这个接口被List的实现类(子类)使用。如果List子类实现了RandomAccess接口,那就表示它能够快速随机访问存储的元素。RandomAccess接口的意义在于:在对列表进行随机或顺序访问的时候,访问算法能够选择性能最佳方式。

  一般的列表访问算法在访问列表元素之前,都被建议先使用instanceof关键字检查一下列表是否是一个RandomAccess子类,然后再决定采用随机还是顺序方式访问列表中的元素,这样可以保证访问算法拥有最佳的性能。

  对于List的子类,如果:

for(int i=0, n=list.size(); i < n; i++){
     list.get(i);       
}

  的访问方式比:

for (Iterator i=list.iterator(); i.hasNext(); ){
    i.next();
}

  快,那么它应该实现RandomAccess接口。

接口源码:

/*
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.util;

/**
 * Marker interface used by <tt>List</tt> implementations to indicate that
 * they support fast (generally constant time) random access.  The primary
 * purpose of this interface is to allow generic algorithms to alter their
 * behavior to provide good performance when applied to either random or
 * sequential access lists.
 *
 * <p>The best algorithms for manipulating random access lists (such as
 * <tt>ArrayList</tt>) can produce quadratic behavior when applied to
 * sequential access lists (such as <tt>LinkedList</tt>).  Generic list
 * algorithms are encouraged to check whether the given list is an
 * <tt>instanceof</tt> this interface before applying an algorithm that would
 * provide poor performance if it were applied to a sequential access list,
 * and to alter their behavior if necessary to guarantee acceptable
 * performance.
 * 
 * <p>It is recognized that the distinction between random and sequential
 * access is often fuzzy.  For example, some <tt>List</tt> implementations
 * provide asymptotically linear access times if they get huge, but constant
 * access times in practice.  Such a <tt>List</tt> implementation
 * should generally implement this interface.  As a rule of thumb, a
 * <tt>List</tt> implementation should implement this interface if,
 * for typical instances of the class, this loop:
 * <pre>
 *     for (int i=0, n=list.size(); i &lt; n; i++)
 *         list.get(i);
 * </pre>
 * runs faster than this loop:
 * <pre>
 *     for (Iterator i=list.iterator(); i.hasNext(); )
 *         i.next();
 * </pre>
 *
 * <p>This interface is a member of the 
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
 * Java Collections Framework</a>.
 *
 * @since 1.4
 */
public interface RandomAccess {
}

转载于:https://www.cnblogs.com/jing99/p/7402213.html

相关文章:

  • python用input输入list_python怎么用input函数输入一个列表
  • sublime text 2 卸载与重装
  • 数字图像中255表示什么_数字图像处理学习之(一):图像与图像的表示
  • 初见Hadoop—- 搭建MyEclipse 访问HDFS 上的文件
  • 有些网站打开一半空白_如何发一条空白的朋友圈
  • 一台服务器能承载多少用户_冰淇淋机多少钱一台?做冰淇淋也能小有成就
  • json web token 网上学习笔记
  • python中函数包括_python函数
  • bzoj1059 [ZJOI2007]矩阵游戏
  • python 直方图的绘制方法全解_python可视化:matplotlib绘制直方图进阶篇!
  • CPU位数、操作系统位数、机器字长、C/C++基本数据类型长度
  • python决策树分类 导入数据集_python中使用scikit-learn和pandas决策树进行iris鸢尾花数据分类建模...
  • 2017/08/22 工作日志
  • 为什么安装了python桌面没有图标_python-pyinstaller不显示图像和图标
  • MySQL Workbench出现:Error Code: 2013. Lost connection to MySQL server during query的问题解决...
  • 【mysql】环境安装、服务启动、密码设置
  • 2018一半小结一波
  • cookie和session
  • C学习-枚举(九)
  • PyCharm搭建GO开发环境(GO语言学习第1课)
  • 第2章 网络文档
  • 欢迎参加第二届中国游戏开发者大会
  • 基于webpack 的 vue 多页架构
  • 聚簇索引和非聚簇索引
  • 力扣(LeetCode)56
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 你真的知道 == 和 equals 的区别吗?
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 使用common-codec进行md5加密
  • 我建了一个叫Hello World的项目
  • 在GitHub多个账号上使用不同的SSH的配置方法
  • 正则与JS中的正则
  • Salesforce和SAP Netweaver里数据库表的元数据设计
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • (3)Dubbo启动时qos-server can not bind localhost22222错误解决
  • (C语言)编写程序将一个4×4的数组进行顺时针旋转90度后输出。
  • (delphi11最新学习资料) Object Pascal 学习笔记---第5章第5节(delphi中的指针)
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (六)激光线扫描-三维重建
  • (五)c52学习之旅-静态数码管
  • (转)socket Aio demo
  • ***汇编语言 实验16 编写包含多个功能子程序的中断例程
  • **PHP二维数组遍历时同时赋值
  • ..thread“main“ com.fasterxml.jackson.databind.JsonMappingException: Jackson version is too old 2.3.1
  • .NET 8.0 中有哪些新的变化?
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .net core使用RPC方式进行高效的HTTP服务访问
  • .NET Core中的去虚
  • .NET 表达式计算:Expression Evaluator
  • .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)
  • .NET6 命令行启动及发布单个Exe文件
  • .NET设计模式(8):适配器模式(Adapter Pattern)