简体   繁体   English

RandomAccess 接口如何在内部工作?

[英]How does RandomAccess interface works internally?

As per my understanding, this RandomAccess interface provides an ability to retrieve data in constant time.根据我的理解,这个 RandomAccess 接口提供了在恒定时间内检索数据的能力。 How this functionality is achieved?这个功能是如何实现的? Is there any hashing technique?有没有散列技术?

Thanks in advance提前致谢

Your understanding is wrong.你的理解是错误的。 RandomAccess may provide a promise but it doesn't provide any ability. RandomAccess可能会提供一个承诺,但它不提供任何能力。 Providing the ability is up to the implementing class.提供能力取决于实现类。 Actually RandomAccess doesn't “work internally” in any sense of the expression, it has no functionality whatsoever.实际上,从表达式的任何意义上来说, RandomAccess都不是“内部工作”,它没有任何功能。

In the documentation it called an indication , not a promise:文档中,它称为指示,而不是承诺:

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. List实现使用的标记接口来指示它们支持快速(通常是恒定时间)随机访问。

The classes implementing RandomAccess generally provide constant time access by storing their contents in an array.实现RandomAccess的类通常通过将其内容存储在数组中来提供恒定时间访问。 Array lookup takes constant time.数组查找需要恒定的时间。 (Classes implementing the interface include ArrayList , AttributeList , CopyOnWriteArrayList , RoleList , RoleUnresolvedList , Stack and Vector according to the documentation. Surely also some third-party classes implement it too.) (根据文档,实现该接口的类包括ArrayListAttributeListCopyOnWriteArrayListRoleListRoleUnresolvedListStackVector 。当然也有一些第三方类也实现了它。)

Edit: Fast access in arrays relies on the hardware.编辑:阵列中的快速访问依赖于硬件。 The computer can access any memory cell (storage cell) in constant time.计算机可以在恒定时间内访问任何存储单元(存储单元)。 Array elements are laid out in consecutive memory cells.阵列元素布置在连续的存储单元中。 So if you know that an array is laid out from address 54321 and you need to access the array element at index 35, you add those two numbers and know the element is at memory address 54356 (I am ignoring the slight complication in that the word size is often 8 bytes and addresses have granularity of 1 byte).因此,如果您知道一个数组是从地址 54321 开始布局的,并且您需要访问索引 35 处的数组元素,那么您将这两个数字相加并知道该元素位于内存地址 54356(我忽略了这个词的轻微复杂性)大小通常为 8 个字节,地址的粒度为 1 个字节)。 The principle is the same whether the JVM is interpreting byte code or the method has been compiled to native machine code thus bypasses the JVM.无论是JVM解释字节码还是方法编译成本地机器码绕过JVM,原理都是一样的。 Your search engine should lead to a better explanation than the one I am giving here.你的搜索引擎应该会给出比我在这里给出的更好的解释。

It's quite general for interfaces they they don't have any internal working but leave this to the classes implementing the interface (as has been said, since Java 8 and 9 the situation has become more blurred, but in this respect RandomAccess still keeps the classical design).对于接口来说,它们没有任何内部工作是很普遍的,但将其留给实现接口的类(如前所述,自 Java 8 和 9 以来,情况变得更加模糊,但在这方面RandomAccess仍然保持经典设计)。 It's the job of an interface to specify a behaviour and that of the implementing class/es to provide it.接口的工作是指定行为,而实现类的工作是提供它。

Edit:编辑:

From the documentation, "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".从文档中可以看出,“此接口的主要目的是允许通用算法改变其行为,以在应用于随机或顺序访问列表时提供良好的性能”。 Why it is mentioned as algorithms here?为什么它在这里被称为算法?

“Generic algortithms” refers to algorithms that you and I write that work with lists that may or may not have fast element access (may or may not implement RandomAccess ). “通用算法”是指你和我编写的算法,这些算法与列表一起工作,这些列表可能有也可能没有快速元素访问(可能实现也可能不实现RandomAccess )。 So you may write for example:所以你可以写例如:

public void yourMethod(List<String> param) {
     if (param instanceof RandomAccess) {
          // do your work relying on fast element access
     } else {
         // do your work in some other way that doesn’t depend on fast element access
         // for example by iterating sequentially through the list
         // or copying the elements to a faster list implementation first.
     }
}

So no, it has nothing to do with different JVM implementations.所以不,它与不同的 JVM 实现无关。

RandomAccess is a marker interface . RandomAccess是一个标记接口 It is an interface with no methods.它是一个没有方法的接口。 Apart from that an interface generally does not have any functionality in it 1 .除此之外,接口通常没有任何功能1 It is upto the implementing classes to provide random access functionality.由实现类来提供随机访问功能。

Have a look at看一下

Why does ArrayList implement RandomAccess Interface? 为什么 ArrayList 实现了 RandomAccess 接口?

What is the use of marker interfaces in Java? Java中标记接口的用途是什么?


1 Yes. 1是的。 An interface can have default methods that has some logic.接口可以具有具有某些逻辑的默认方法。 But this was added only from Java 8 to enable adding new methods to an interface without breaking the implementing classes.但这只是从 Java 8 添加的,以便在不破坏实现类的情况下向接口添加新方法。 An interface is only for providing or declaring the contract that the implementing class has to fulfil.接口仅用于提供或声明实现类必须履行的契约。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM