`
kanpiaoxue
  • 浏览: 1777490 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

关于jdk的java.util.RandomAccess

 
阅读更多

 

RandomAccess是一个声明接口,主要用来区分对List不同实例的访问策略。比如ArrayList是顺序访问的策略最快,也就是RandomAccess随机访问;LinkedList是链表实现,使用迭代器一个个的访问,不去实现RandomAccess接口,表示它不支持随机访问(下标位置)。具体内容看下面jdk的描述。在使用List的接口的时候最好可以根据RandomAccess来判断实例的访问策略,加快访问list的性能。

举例代码如下:

    public static <T> List<T> unmodifiableList(List<? extends T> list) {
        return (list instanceof RandomAccess ?
                new UnmodifiableRandomAccessList<>(list) :
                new UnmodifiableList<>(list));
    }

 

 

jdk的原文描述 写道
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();

This interface is a member of the Java Collections Framework.

Since:
1.4

 

 

 

分享到:
评论

相关推荐

    Addison.Wesley.The.Java.Programming.Language.4th.Edition.Aug.2005.chm

    It also covers most of the classes in the main packages (java.lang, java.util, java.io) as implemented in the J2SE™ Development Kit 5.0 (more commonly known as JDK 5.0, or in the older nomenclature ...

    JDK 1.5的泛型實現(Generics in JDK 1.5)

    #002 implements List, RandomAccess, #003 Cloneable, java.io.Serializable #004 { #005 private transient E[] elementData; #006 private int size; #007 public ArrayList(int initialCapacity) { #008 ...

    java1.8源码-jdk1.8.0_151-:阅读Java源码,版本为jdk1.8.0_151,将会同步翻译源码中的文档注释

    java 1.8 源码 jdk1.8.0_151-源码的中文翻译和一些自己的理解 声明 作者现在大四快要毕业,在实习中,为了在未来成为一名架构师,下定决心开始读Java的...RandomAccess.java ArrayList.java LinkedList.java Vector.jav

    java7帮助文档

    The Nimbus Look and Feel has been moved from the com.sun.java.swing package to the javax.swing package; see the javax.swing.plaf.nimbus package. Mixing Heavyweight and Lightweight Components is ...

    java编码规范考试题答案.doc

    7. **随机数生成**:`java.util.Random` 类适用于一般的随机数生成需求,如验证码生成。在挑战算法或高安全性的随机数需求时,可能需要使用更专业的随机数生成库。正确选项为 B(验证码的随机数生成)。 8. **...

    Java邮件开发Fundamentals of the JavaMail API

    addition, you will need a development environment such as the JDK 1.1.6+ or the Java 2 Platform, Standard Edition (J2SE) 1.2.x or 1.3.x. A general familiarity with object-oriented programming ...

    JAVA编程中常用的英文单词词汇汇总.doc

    3. **JDK (Java Development Kit)**:Java开发工具包,包含了编写、编译和运行Java程序所需的所有工具。 4. **JVM (Java Virtual Machine)**:Java虚拟机,负责解释和执行Java字节码。 5. **Compile**:编译,将源...

    (完整word)java必备单词.doc

    5. **JDK**: Java Development Kit,Java开发工具包 - 用于编写、测试和调试Java程序的一套软件工具。 6. **JVM**: Java Virtual Machine,Java虚拟机 - 解释和执行Java字节码的平台独立环境。 7. **Compile**: ...

    JAVA必备英文单词.docx

    3. **JDK (Java Development Kit)**:Java开发工具包,包含了编译、运行Java程序所需的工具和库。 4. **JVM (Java Virtual Machine)**:Java虚拟机,它是Java程序的运行环境,负责解释和执行字节码。 5. **Compile...

    2021-2022计算机二级等级考试试题及答案No.10226.docx

    18. Date、Calendar和DateFormat类都是Java中处理日期和时间的类,位于`java.util`包内。 19. TCP是面向连接的协议,需要明确客户端和服务器端;它提供可靠无差错的数据传输。UDP是无连接的,资源消耗小,通信效率...

    2021-2022计算机二级等级考试试题及答案No.19235.docx

    11. 集合与数组工具类:Java的`java.util`包中提供了`Collections`类用于操作集合,`Arrays`类用于操作数组。 12. 数组创建:正确创建数组的Java代码是`int[] myArray={1,2,3,4,5};`(C)。 13. 数据库模型历史:最...

    JAVA编程常用英文单词汇总.doc

    3. **JDK**: Java Development Kit(JAVA开发工具包)— 是JAVA的官方开发环境,包含了JAVA运行环境(JRE)、JAVA工具和JAVA基础类库等。 4. **JVM**: Java Virtual Machine(JAVA虚拟机)— 是一个可以执行JAVA字节...

    Java基础常见英语词汇

    3. **JDK (Java Development Kit)**:Java开发工具包,包含了编写、编译、调试Java程序所需的所有工具,包括JRE(Java Runtime Environment)和开发工具。 4. **JVM (Java Virtual Machine)**:Java虚拟机,执行...

    java词汇解释

    在Java中,这个过程通常是指将.java文件编译成.class文件。 #### Run 运行,指的是执行程序的过程。在Java中,经过编译后的字节码由JVM解释执行。 #### Class 类,是Java编程的基本单元之一,用来定义对象的属性和...

    java集合(自学整理)

    implements List, RandomAccess, Cloneable, java.io.Serializable ``` 数组的初始容量默认为 10: ```java private static final int DEFAULT_CAPACITY = 10; ``` #### 扩容机制 当向 `ArrayList` 添加元素时...

    JAVA必备英文单词.pdf

    * JDK: Java 开发工具包 * Compile: 编译 * System: 系统 * Variable: 变量 * Method: 方法 * Get: 得到 * Default: 默 认 * Print: 打印 * Line: 行 * Operation: 操作 * Array: 数组 * Member-variable: 成员变量 ...

Global site tag (gtag.js) - Google Analytics