Why does ArrayList implement RandomAccess Interface?

你离开我真会死。 提交于 2019-12-18 14:08:46

问题


ArrayList implements RandomAccess interface. RandomAccess interface has no methods. When I checked LinkedList it does not implement RandomAccess interface.

So in case of ArrayList, what is the point of implementing it?


回答1:


Interfaces with no methods are called marker interfaces in Java.

As per the JavaDoc of RandomAccess:

Marker interface used by List implementations to indicate
that they support fast (generally constant time) random access.

For more information check the two JavaDoc pages.

http://docs.oracle.com/javase/6/docs/api/java/util/RandomAccess.html

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html




回答2:


RandomAccess interface has no method

This is called a marker interface and is a design pattern called marker interface pattern.

When I checked LinkedList it does not implement RandomAccess interface. So in case of ArrayList what is the point of implementing it?

Because random access in a LinkedList is O(n), while it's O(1) in an ArrayList.

It's stated in the doc :

The best algorithms for manipulating random access lists (such as ArrayList) can produce quadratic behavior when applied to sequential access lists (such as LinkedList)




回答3:


This seems pretty well described in the documentation: http://docs.oracle.com/javase/7/docs/api/java/util/RandomAccess.html

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



回答4:


1) There are two classes which implement RandomAccess Interface. They are:

ArrayList (Part of List<I>)
Vector    (Part of List<I>)

2) The purpose of RandomAccess Interface is to retrieve any random element in the Collection at the same speed. Example: I have a collection of 1 million objects. Implementing RandomAccess interface makes your time to retrieve the 10th element and 17869th element the same. This makes ArrayList and Vector more powerful.

3) RandomAccess Interface has no methods or fields and is also called a Marker Interface. These are used to indicate something to the compiler, in other words implementing these interfaces is meant to imply some special treatment of the implementing class.




回答5:


Let's see how you can practically make use of this marker interface. First the relevant parts from docs (bolds are mine just to emphasise).

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. RandomAccess 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).

Let's take an example. Suppose you are writing a generic implementation of algorithm such as sort and you are choosing quick sort as you want in-place sorting. Why generic ? Because maybe you want the algorithm to be able to work with reasonable and predictable performance characteristics on all kinds of List (there are many kinds out there - aren't). So your algorithm function would take a list as input and return the same list as sorted. For a moment let's leave aside the various factors which influence the performance of quick sort (such as already sorted data, repeated data, right choice of pivot etc.). Apart from the above characteristics of the data/sort another crucial point is that your algorithm makes heavy us of traversing the list - in this case it makes use of random access heavily as it needs to compare and swap elements. So what do you think - will your algorithm perform well on all types of List implementations. Think about LinkedList - there is a provision for random access in the API but it needs lots of traversals because of the very nature of how data is organised in list as nodes pointing to each other and the painful unavoidable act of going through large number of nodes to arrive at a random node. So your generic algorithm has more variability in terms of performance. What if you knew somehow that some Lists provide fast ramdom access and some do not. What if have a marker which says so. In comes the "RandomAccess" marker interface which is implemented (marked/annotated would be better word) by ArrayList to indicate (your code will typically check with instanceof RandomAccess test) that it's quite fast to access randomly it's data and you can rely on that to write an algorithm which performs and if some list does not then try an alternate algorithm or maybe convert it first into ArrayList or at worst reject such an input entirely.

Another part of doc deals with what is considered to be fast random by giving two different ways to access the underlying data which is easy to understand. Fast random access means the first runs faster than second at all times.

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



回答6:


RandomAccess: Marker interface

java.util.RandomAccess - Since JDK 1.4, member of collection framework. Implemetations: ArrayList and CopyOnWriteArrayList.

For Random access of data (Index basis)

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.

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

For more information, see my blog:
http://javaexplorer03.blogspot.in/2015/07/randomaccess-java.html




回答7:


RandomAccess interface signifies an Efficient Random access to elements of Collection.

In client code, you can check whether the collection is instance of RandomAccess and then only perform the Random access operations.

Elements from both LinkedList and ArrayList can be accessed randomly however ArrayList complexity is O(1) and LinkedList is O(n).




回答8:


The interface has no methods(Marker Interface), but you can use it to test whether a particular collection supports efficient random access

Collection<Integer> c = new ArrayList<Integer>();
if (c instanceof RandomAccess)
{
        System.out.println("use random access algorithm -> like ArrayList");//fast access date
}
else
{
        System.out.println("use sequential access algorithm -> like LinkedList");//fast delete data 
}


来源:https://stackoverflow.com/questions/20869993/why-does-arraylist-implement-randomaccess-interface

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!