Java编程:RandomAccess接口
RandomAccess 是一个标记接口,标识实现该接口的类支持快速随机访问。
一、随机访问和串行访问
举个例子:在 Java 中,有 ArrayList 和 LinkedList 两种数据类型。
- ArrayList 是基于数组实现的,可以通过数组下表灵活的访问列表中的任意元素。像 ArrayList 这种设计,我们就称之为随机访问。
- LinkedList 是基于 next 引用实现的,只能通过串行的方式,从头到尾逐个查找目标元素。像 LinkedList 这种设计,我们称之为串行访问。
二、快速随机访问
根据 RandomAccess 的注释理解,如果“代码一”的执行速度比“代码二”快,则应该实现 RandomAccess 接口。
换句话说,如果集合类(List)实现了 RandomAccess 接口,则采用“代码一”的方式能够获得更高的执行效率。这也是为什么阿里巴巴的代码规约中推荐使用“代码一”的方式进行集合遍历。
代码一:
for (int i=0, n=list.size(); i < n; i++)
list.get(i);
代码二:
for (Iterator i=list.iterator(); i.hasNext(); )
i.next();
通过查看源代码,我们会发现 ArrayList 实现了 RandomAccess 接口,LinkedList 则没有实现,具体原因我们可以分析一个两个类的 get 方法,在这里我们不进行具体分析,大家自己看源码哈。
三、源代码
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. * 翻译:被 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. * 翻译:将随机访问 lists(如 ArrayList)的最好算法应用于顺序访问 lists(如 LinkedList) * 时,可能会产生二次行为(我的理解:这里的二次行为指的是通过遍历 LinkedList ,找到目标对象, * 这比通过 ArrayList 银锁直接获取目标对象,多了一个遍历行为,这个遍历行为就是二次行为)。 * * 如果将随机访问算法应用于串行访问 list 时,将会产生较差的性能。鼓励通用列表算法在应用之前, * 先检查 List 是否实现了 RandomAccess 接口,如果实现该接口,则采用快速随机访问法进行遍历, * 否则使用迭代器的方式进行访问。 * * <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: * algorithm1: * <pre> * for (int i=0, n=list.size(); i < n; i++) * list.get(i); * </pre> * runs faster than this loop: * algorithm2: * <pre> * for (Iterator i=list.iterator(); i.hasNext(); ) * i.next(); * </pre> * 翻译:人们认识到随访访问和串行访问的区别通过是模糊的。例如,如果 List 变得很大, * 则提供渐近线性的访问时间,然而在实践中基本上是常量访问时间,像这样的 List 应该实现 * RandomAccess 接口。根据经验,如果“算法一”的访问速度比“算法二”快,则 List 应该实现 * RandomAccess 接口。 * <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 {
}
在 Java 中,每一个类文件都是大牛们不断总结和提炼出来的,都值得我们认真学习和思考。文章内容仅代表个人观点,如有不正之处,欢迎批评指正,谢谢大家。
还没有评论,来说两句吧...