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

java空接口_java中RandomAccess接口明明是个空接口,有什么用呢?

RandomAccess接口

一、官方描述

首先我们先看一下这个接口在java中是怎么描述的。

位置:rt.jar中java.util.RandomAccess

/**

* Marker interface used by List 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.

*

*

The best algorithms for manipulating random access lists (such as

* ArrayList) can produce quadratic behavior when applied to

* sequential access lists (such as LinkedList). Generic list

* algorithms are encouraged to check whether the given list is an

* instanceof 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.

*

*

It is recognized that the distinction between random and sequential

* access is often fuzzy. For example, some List implementations

* provide asymptotically linear access times if they get huge, but constant

* access times in practice. Such a List implementation

* should generally implement this interface. As a rule of thumb, a

* List implementation should implement this interface if,

* for typical instances of the class, this loop:

*


 

* for (int i=0, n=list.size(); i < n; i++)

* list.get(i);

*

* runs faster than this loop:

*


 

* for (Iterator i=list.iterator(); i.hasNext(); )

* i.next();

*

大致的意思是说:这是一个用于List的实现类的中使用的标记接口,能够支持快速随机访问(一定时间内)。此接口的主要目的是允许通用算法在应用于随机或顺序访问列表时改变其行为以提供良好的性能。

只在List的实现类ArrayList可以for + get(index)使用该算法执行,在LinkedList类中不行。

二、查看ArrayList和LinkedList

位置:rt.jar中java.util.ArrayList和java.util.LinkedList

2.1 ArrayList

public class ArrayList extends AbstractList

implements List, RandomAccess, Cloneable, java.io.Serializable

{

private static final long serialVersionUID = 8683452581122892189L;

...

}

2.2 LinkedList

public class LinkedList

extends AbstractSequentialList

implements List, Deque, Cloneable, java.io.Serializable

{

transient int size = 0;

...

}

由源码可以看出:ArrayList是一个实现了List、RandomAccess、Cloneable、 java.io.Serializable等接口的类。实现可以随机访问、克隆及序列化的数组集合。

LinkedList则是实现了List、Deque、Cloneable、java.io.Serializable等接口的类,所以它是一个克隆、序列化的双端连表。

三、验证RandomAccess实践,当然选择ArrayList。

代码如下:

public class Application {

public static void main(String[] args) {

System.out.println("开始准备数据");

List data = new ArrayList();

int length = 50000000;

while (length > 0){

data.add(length);

length--;

}

System.out.println("准备数据完成");

System.out.println("开始使用get(index)");

Long timeIndex = System.currentTimeMillis();

for (int i = 0; i < data.size(); i++){

data.get(i);

}

System.out.println("结束使用get(index) :" + String.valueOf(System.currentTimeMillis() - timeIndex));

System.out.println("开始使用Iterable");

Long timeIterable = System.currentTimeMillis();

Iterator iterator = data.iterator();

while (iterator.hasNext()){

iterator.next();

}

System.out.println("结束使用Iterable :" + String.valueOf(System.currentTimeMillis() - timeIterable));

}

}

运行结果:

开始准备数据

准备数据完成

开始使用get(index)

结束使用get(index) :4

开始使用Iterable

结束使用Iterable :8

四、总结

可以看出当集合的数量达到一定值的时候,效果很明显,但是当length小的时候结果不是很明显。实际使用中看自己的场景使用即可。本身对于程序性能影响并不大。

相关文章:

  • java pdf base64_Javaweb中利用pdf.js显示oracle数据库中Base64编码转为的PDF
  • SQL Server 2005豪华盛宴
  • java班级管理系统代码_基于jsp的班级管理系统-JavaEE实现班级管理系统 - java项目源码...
  • java 聚合服务_远程服务接口聚合带来的性能提升
  • java编写客户_java编写服务器和客户端.docx
  • ORA-03113错误分析
  • 一个带年月日时分秒选择的js脚本
  • java发送苹果消息慢_使用javapns向苹果apns发送消息成功,手机设备有时收不到推送信息...
  • java 1.6 ubuntu_Ubuntu16.04安装java6(jdk 1.6)
  • 自适应反走样(Adaptive Anti-aliasing)
  • java emptyarray_Java ArrayDeque isEmpty()方法
  • GCCfor Win32 开发环境介绍(6)
  • php 禁止直接访问目录_php – 防止网站某些目录被直接访问的最佳方法是什么?...
  • Delphi关于改进AES算法的加解密文件功能
  • php博客系统答辩ppt,个人博客系统设计与实现答辩PPT.ppt
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • [iOS]Core Data浅析一 -- 启用Core Data
  • CentOS7 安装JDK
  • es6(二):字符串的扩展
  • ESLint简单操作
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • golang 发送GET和POST示例
  • interface和setter,getter
  • JavaScript学习总结——原型
  • Java反射-动态类加载和重新加载
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • js ES6 求数组的交集,并集,还有差集
  • JSONP原理
  • JS数组方法汇总
  • markdown编辑器简评
  • mysql 5.6 原生Online DDL解析
  • QQ浏览器x5内核的兼容性问题
  • Redis中的lru算法实现
  • SpringCloud集成分布式事务LCN (一)
  • tab.js分享及浏览器兼容性问题汇总
  • Vue.js 移动端适配之 vw 解决方案
  • vue学习系列(二)vue-cli
  • 检测对象或数组
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 目录与文件属性:编写ls
  • 设计模式(12)迭代器模式(讲解+应用)
  • 使用parted解决大于2T的磁盘分区
  • 使用putty远程连接linux
  • 我的zsh配置, 2019最新方案
  • 移动端解决方案学习记录
  • 原生 js 实现移动端 Touch 滑动反弹
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • postgresql行列转换函数
  • Spring Batch JSON 支持
  • ​configparser --- 配置文件解析器​
  • ​LeetCode解法汇总2182. 构造限制重复的字符串
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • #NOIP 2014#Day.2 T3 解方程
  • #stm32驱动外设模块总结w5500模块
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)