java 中RandomAccess接口源码分析

 更新时间:2017年05月28日 08:59:08   投稿:lqh  
这篇文章主要介绍了java 中RandomAccess接口源码分析的相关资料,需要的朋友可以参考下

java 中RandomAccess接口源码分析

RandomAccess是一个接口,位于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.
 * List实现所使用的标记接口,用来表明实现了这些接口的list支持快速(通常是常数时间)随机访问。
 * 这个接口的主要目的是允许一般的算法更改它们的行为,以便在随机或者顺序存取列表时能提供更好的性能。
 * <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.
 * 操作随机访问列表(如ArrayList)的最佳算法在应用于顺序存取列表时,有可能产生二次项行为。
 * 泛型算法列表鼓励在将某个算法应用于顺序存取列表可能导致差的性能之前,先检查给定的列表是否是这个接口的一个实例,
 * 并在需要时去改变这些算法的行为以保证性能。
 * <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:

 * 随机访问和顺序存取之间的界限通常是模糊的。例如,一些List实现在变得很大时会导致渐进的非线性访问时间,但实际上是常量访问时间。
 * 这样的List实现通常都应该实现该接口。
 * 一般来说,某个List实现如果(对某些典型的类的实例来说)满足下面的条件,就应该实现这个接口:循环
 * <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" rel="external nofollow" >
 * Java Collections Framework</a>.
 * 这个接口是Java集合框架的一员。
 * @since 1.4
 */
public interface RandomAccess {
}

 RandomAccess是一个空接口,而空接口的作用一般是起到一个标识的作用。

通俗点讲,就是判断一个list是否实现了RandomAcess接口,如果实现了,采用下面所示的简单的for循环进行访问速度比较快:

for (int i=0, n=list.size(); i &lt; n; i++)
   list.get(i);

如果未实现RandomAcess接口,则采用下面的iterator循环访问速度比较快。

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

判断使用instanceof,即

 if (list instanceof RandomAccess) 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • 基于Java实现PDF文本旋转倾斜

    基于Java实现PDF文本旋转倾斜

    这篇文章主要介绍了基于Java实现PDF文本旋转倾斜,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • SpringBoot整合Shiro的代码详解

    SpringBoot整合Shiro的代码详解

    shiro是一个权限框架,它提供了很方便的权限认证和登录的功能.下面通过本文给大家分享SpringBoot整合Shiro的代码详解,需要的的朋友参考下吧
    2017-08-08
  • MybatisPlus创建时间不想用默认值的问题

    MybatisPlus创建时间不想用默认值的问题

    MybatisPlus通过FieldFill注解和MpMetaObjectHandler类支持自动填充字段功能,特别地,可以设置字段在插入或更新时自动填充创建时间和更新时间,但在特定场景下,如导入数据时,可能需要自定义创建时间
    2024-09-09
  • SpringBoot本地磁盘映射问题

    SpringBoot本地磁盘映射问题

    这篇文章主要介绍了SpringBoot本地磁盘映射问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • 详解Java中两种分页遍历的使用姿势

    详解Java中两种分页遍历的使用姿势

    这篇文章主要介绍了详解Java中两种分页遍历的使用姿势,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Java微信公众平台开发(12) 微信用户信息的获取

    Java微信公众平台开发(12) 微信用户信息的获取

    这篇文章主要为大家详细介绍了Java微信公众平台开发第十二步,微信用户信息的获取,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • java反射简单实例

    java反射简单实例

    这篇文章主要介绍了java反射机制,以一个简单实例形式分析了Java反射的原理与实现技巧,需要的朋友可以参考下
    2015-02-02
  • 基于springcloud异步线程池、高并发请求feign的解决方案

    基于springcloud异步线程池、高并发请求feign的解决方案

    这篇文章主要介绍了基于springcloud异步线程池、高并发请求feign的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • SpringBoot中的yaml语法及静态资源访问问题

    SpringBoot中的yaml语法及静态资源访问问题

    这篇文章主要介绍了SpringBoot中的yaml语法及静态资源访问问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-07-07
  • java+selenium 网易云音乐刷累计听歌数的方法

    java+selenium 网易云音乐刷累计听歌数的方法

    这篇文章主要介绍了java+selenium 网易云音乐刷累计听歌数的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06

最新评论