版本
JDK8(JDK1.8)
RandomAccess接口源码重点
RandomAccess接口是一个标记接口,用于指示实现该接口的列表支持快速随机访问,
如数组实现的列表就支持快速随机访问,而链表实现的列表就不支持快速随机访问
RandomAccess接口源码
/**
* Marker interface used by {@code 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.
* List实现使用的标记接口,用于指示它们支持快速(通常为常数时间)随机访问。
* 此接口的主要目的是允许通用算法在应用于随机或顺序访问列表时改变其行为,以提供良好的性能。
*
*
* <p>The best algorithms for manipulating random access lists (such as
* {@code ArrayList}) can produce quadratic behavior when applied to
* sequential access lists (such as {@code LinkedList}). Generic list
* algorithms are encouraged to check whether the given list is an
* {@code 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.
* 操作随机访问列表(如ArrayList)的最佳算法在应用于顺序访问列表(如LinkedList)时会产生二次行为。
* 鼓励通用列表算法在应用算法之前检查给定列表是否为instanceof此接口,
* 如果将该算法应用于顺序访问列表,则该算法将提供较差的性能,
* 并在必要时改变其行为,以确保可接受的性能。
*
*
*
*
*
*
* <p>It is recognized that the distinction between random and sequential
* access is often fuzzy. For example, some {@code List} implementations
* provide asymptotically linear access times if they get huge, but constant
* access times in practice. Such a {@code List} implementation
* should generally implement this interface. As a rule of thumb, a
* {@code List} 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 < n; i++)
* list.get(i);
* </pre>
* for (int i=0, n=list.size(); i < n; i++)
* list.get(i);
*
* runs faster than this loop:
* 运行速度比此循环快:
* <pre>
* for (Iterator i=list.iterator(); i.hasNext(); )
* i.next();
* </pre>
* for (Iterator i=list.iterator(); i.hasNext(); )
* i.next();
*
* <p>This interface is a member of the
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
* Java Collections Framework</a>.
*
* @since 1.4
*/
public interface RandomAccess {
}