简体   繁体   English

什么是RandomAccess? 为什么我们使用RandomAccess?

[英]What basically is RandomAccess? Why we use RandomAccess?

I was studying collections in java in between I got stuck a point that some classes like ArrayList implements RandomAccess too while some classes don't. 我当时在研究Java中的集合,但我发现一个问题是,诸如ArrayList之类的某些类也实现RandomAccess,而某些类则没有。 I want to know that why this interface is implemented and what its benefit? 我想知道为什么要实现此接口及其好处? What would happen if I use this Interface in my class? 如果我在课堂上使用此接口会怎样?

Random Access List vs Sequential List 随机访问列表与顺序列表

Random Access List is a list where you can access any random data in a constant and a faster pace whereas sequential list is where you have to iterate sequentially through all the item before it, to access any particular item. 随机访问列表是一个列表,您可以在其中以恒定的速度更快地访问任何随机数据,而顺序列表是必须依次遍历其之前的所有项目以访问任何特定项目的列表。

As in the below graphical image, you can see that in first example, if you want to access 9, you can directly get the values using index whereas in the second image, the data can not be accessed randomly and needs to be iterated through 23 -> 3 -> 17 -> 9 or 42 -> 9 and hence time in accessing any value in the second case is not constant and differs for each value. 如下面的图形图像所示,您可以看到在第一个示例中,如果要访问9,则可以直接使用index获取值,而在第二个图像中,数据不能被随机访问,需要遍历23 -> 3 -> 17 -> 942 -> 9 ,因此第二种情况下访问任何值的时间不是恒定的,并且每个值都不同。

在此处输入图片说明

Why RandomAccess interface is implemented? 为什么要实现RandomAccess接口?

Introduced as part of JDK 1.4, it is a marker interface which indicates that the class implementing it takes constant and fast time in accessing any random data in list. 作为JDK 1.4的一部分引入的,它是一个标记器接口,它指示实现它的类在访问列表中的任何随机数据时花费恒定且快速的时间。

What is the benefit? 有什么好处?

Any implementation of the algorithm can check if the current list is a child of Random Access and accordingly ensure the best performance for random access or sequential access. 该算法的任何实现都可以检查当前列表是否为“随机访问”的子级,从而确保随机访问或顺序访问的最佳性能。

Below is one great example from one blog which explains one of the usage in a great way: 以下是一个博客中的一个很好的示例,该示例很好地解释了一种用法:

Object o;
if (listObject instanceof RandomAccess)
{
  for (int i=0, n=list.size(); i < n; i++)
  {
    o = list.get(i); // directly get the object as list implements Random access
    //do something with object o
  }

}
else
{
  Iterator itr = list.iterator();
  for (int i=0, n=list.size(); i < n; i++)
  {
    o = itr.next(); // Use iterator to get values sequentially as random access 
                    //  is not fast for this list and hence does not implement RandomAccess
    //do something with object o

  }
}

What would happen if I use this Interface in my class? 如果我在课堂上使用此接口会怎样?

Same will happen as happens when you implement any other marker interface. 当您实现任何其他标记接口时,也会发生同样的情况。 It just gives an indication to other class as what can be expected from the class implementing it. 它只是给其他类一个指示,因为实现它的类可以期望什么。

You can also refer Java docs to understand more about it. 您也可以参考Java文档以了解更多信息。

RandomAccess is a marker interface . RandomAccess标记接口 It doesn't have any methods in it. 它没有任何方法。 Its marks ArrayList to show that it supports random access of any value in O(1) given index. 它的标记ArrayList表示它支持对给定索引O(1)的任何值进行随机访问。

LinkedList , on the other hand, doesn't provide O(1) random access to its elements because its backed by a doubly linked list (hence O(n) random access complexity). 另一方面, LinkedList 提供对其元素的O(1)随机访问,因为它由双链表支持(因此O(n)随机访问复杂度)。 Hence it doesn't make sense to use RandomAccess for LinkedList . 因此,将RandomAccess用于LinkedList没有意义。 Same reasoning can be given to other Collection classes that doesn't implement RandomAccess . 可以对没有实现 RandomAccess其他Collection类给出相同的推理。

I want to know that why this interface is implemented and what its benefit? 我想知道为什么要实现此接口及其好处?

Java collection framework uses this interface to optimize performance . Java收集框架使用此接口来优化性能

Straight out of docs: 直接出文档:

As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop: 根据经验,对于类的典型实例,如果存在以下循环,则List实现应实现此接口:

  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(); 

Also see this javadoc here which explains it beautifully. 另请参阅此处的Javadoc,它对此进行了精美的解释。

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

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