In the JDK source code has a randomaccess interface, this interface does not have any method to implement, then it is what to use.
Public interface Randomaccess {
}
The official documentation explains the following:
Interface randomaccess are used by the list implementation to indicate that they support fast (usually constant) random access. The primary purpose of this interface is to allow a common algorithm to change its behavior to provide good performance when applied to random or sequential access lists.
The best algorithm for dealing with random access lists, such as ArrayList, can be applied to a sequential access list (such as LinkedList) when the behavior occurs two times. The Universal list algorithm is encouraged to check whether a given list is an instance of this interface, and then apply an algorithm that, if applied to the sequential access list, will provide poor performance and, if necessary, change its behavior to ensure acceptable performance.
Recognize that the distinction between random and sequential access is usually blurred. For example, some list implementations gain significant access time in the context of asymptotically linear access time, but in practice the access time does not change. Such a list implementation should typically implement this interface. As a rule of thumb, if for a typical class instance, the list implementation should implement this interface.
The root causes are:
for (int i = 0,n = List.size (); I <n; i + +)
list.get (Ⅰ);
Run faster than this cycle:
for (Iterator i = List.iterator (); I.hasnext ();
I.next ();
My own understanding is this.
Containers typically have two implementations, one is an array, one is the linked list, for the array, random access (subscript access) is the fastest, but for the realization of the form of linked lists, random access (subscript access) is very slow, because the need for the beginning of the node to gradually access the corresponding subscript node.
So in order to improve access efficiency, this interface is used to differentiate whether the current class allows random access.
Let's look at the class declarations of the two class ArrayList and LinkedList that are most commonly used.
ArrayList:
public class Arraylist<e> extends abstractlist<e>
implements List<e>, Randomaccess, Cloneable, Java.io.Serializable
{...}
LinkedList:
public class linkedlist<e>
extends abstractsequentiallist<e>
implements List<e>, Deque <e>, cloneable, java.io.Serializable
{...}
One of the interfaces implemented by ArrayList is randomaccess, but LinkedList is not, so ArrayList's random access efficiency is high, as is actually the case, because ArrayList stores elements in an array.
The BinarySearch () method of the tool class collections in the collection framework is to use this feature to raise the performance.
public class Collections {
...
public static <T>
int BinarySearch (list<? extends Comparable<? super t>> list, T key) {
if (list instanceof Randomaccess | | List.size () <binarysearch_threshold) return
collections.indexedbinarysearch (list, key);
else return
Collections.iteratorbinarysearch (list, key);
...
}
Next we look at the source code for the Indexedbinarysearch and Iteratorbinarysearch methods:
Indexedbinarysearch
private static <T>
int indexedbinarysearch (list<? extends Comparable<? Super t>> List, T key) {
int low = 0;
int high = List.size ()-1;
While [low <= high] {
int mid = (low + high) >>> 1;
Get the element value directly according to subscript
comparable<. Super t> Midval = List.get (mid);
int cmp = Midval.compareto (key);
if (CMP < 0) Low
= mid + 1;
else if (CMP > 0) High
= Mid-1;
else return
mid;//key found
} return
-(low + 1); Key not found
}
Iteratorbinarysearch
private static <T>
int iteratorbinarysearch (list<? extends Comparable<? Super t>> List, T key)
{
int low = 0;
int high = List.size ()-1;
listiterator<? Extends Comparable<? Super T>> i = List.listiterator ();
While [low <= high] {
int mid = (low + high) >>> 1;
Finds element comparable< based on iterators
; Super t> midval = Get (i, mid);
int cmp = Midval.compareto (key);
if (CMP < 0) Low
= mid + 1;
else if (CMP > 0) High
= Mid-1;
else return
mid;//key found
} return
-(low + 1); Key not found
}
The Indexedbinarysearch method is to access the elements in the form of the following, and Iteratorbinarysearch is accessed in the form of iterators, in line with our previous conclusions.