RandomAccess接口

在继续讲解ArrayList源码之前,我们先看一些其他的类和接口。

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">
 * 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) 
时间: 2024-08-04 05:44:50

RandomAccess接口的相关文章

Java集合类:&quot;随机访问&quot; 的RandomAccess接口

引出RandomAccess接口 如果我们用Java做开发的话,最常用的容器之一就是List集合了,而List集合中用的较多的就是ArrayList 和 LinkedList 两个类,这两者也常被用来做比较.因为最近在学习Java的集合类源码,对于这两个类自然是不能放过,于是乎,翻看他们的源码,我发现,ArrayList实现了一个叫做 RandomAccess 的接口,而 LinkedList 是没有的, public class ArrayList<E> extends AbstractLi

关于接口 RandomAccess

今天看到java.util.Collections这个工具类中的 public static <T> void fill(List<? super T> list, T obj) { int size = list.size(); if (size < FILL_THRESHOLD || list instanceof RandomAccess) { // 这一行 for (int i=0; i<size; i++) list.set(i, obj); } else {

RandomAccess标记接口

一.RandomAccess为标记型接口 实现有RandomAccess接口的类可以随机访问 随机访问和顺序访问效率有很大差别 二.案例 编写数据库查询语句时优化写法 原文地址:https://www.cnblogs.com/zhihaospace/p/12554322.html

Java 之 List&lt;T&gt; 接口的实现:ArrayList

Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap ArrayList 是List<T> 下,java的自实现类之一,属于线程非安全的类型,多线程环境下可以考虑用Collections.synchronizedList(List l)函数返回一个线程安全的ArrayList类,也可以使用concurrent并发包下的CopyOnWriteArrayList类,还可

内功心法 -- Java标记接口

写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------这篇博客主要来谈谈"Java标记接口"的相关知识,主要内容包括: 1. 概述 2. Serializable 3. Cloneable 4. RandomAccess --------------------------------------------------------------------1.概述 Ja

为什么这些java接口没有抽象方法?浅谈Java标记接口

在jdk的源码中,存在这样的一些接口,他们不包含任何的(抽象)方法,但是却广泛的存在. 这种接口我们称之为Mark Interface,也就是标记接口. 这些接口呢,我们不用来实现任何的方法,他们的作用就是当某个类实现这个接口的时候,我们就认为这个类拥有了这个接口标记的某种功能了. 下面通过三个例子,分别介绍java中常用的三个标记接口: RandomAccess .Cloneable.java.io.Serializable (1)RandomAccess  在C#中经常会有很多人在争论,在遍

List接口实现类-ArrayList、Vector、LinkedList集合深入学习以及源码解析

学习List接口实现类 ArrayList  Vector  LinkedList List接口的实现类中最常用最重要的就是这三个:ArrayList.Vector.LinkedList. JDK中这三个类的定义: public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { <span st

Java的Stack类实现List接口真的是个笑话吗

今天在网上闲逛时看到了这样一个言论,说“Java的Stack类实现List接口的设计是个笑话”. 当然作者这篇文章的重点不是这个,原本我也只是一笑置之,然而看评论里居然还有人附和,说“Java那种Stack的设计作为笑话,差不多可以算公案了”,我就有点不淡定了,为什么.什么时候“作为笑话”的并且“差不多可以算公案”了呢? 因此我决定写一篇文章来谈谈这个问题. 接口是什么 狭义地讲,接口就是一个类所定义的方法(方法名.参数.返回值).一个类提供了Foo方法,其他类就可以调用它.广义上讲,接口可以理

使用Serializable接口进行JAVA的序列化和反序列化

OBJECT STREAMS – SERIALIZATION AND DESERIALIZATION IN JAVA EXAMPLE USING SERIALIZABLE INTERFACE Hitesh Garg | November 7, 2014 | io | 9 Comments In the previous java tutorials I have discussed about basic of java streams, byte streams, then a modifie