The definition of the RandomAccess interface in the jdk document is as follows: public interface RandomAccess
The following is the translation of jdk's annotation
List implements the tagging interface used to indicate that it supports fast (usually fixed-time) random access. The main purpose of this interface is to allow general algorithms to change their behavior, thereby providing good performance when applied to random or continuous access lists.
When applying the best algorithm for operating random access lists (such as ArrayList) to a continuous access list (such as LinkedList), the behavior of the quadratic term can be generated. If an algorithm is applied to a continuous access list, it is encouraged to use a general list algorithm to check whether a given list is an instanceof of this interface before applying an algorithm that may provide poor performance, and to change its behavior if acceptable performance is required.
It has now been recognized that the difference between random and continuous access is often vague. For example, if the list is large, some List implementations provide incremental linear access time, but are actually fixed access time. Such a List implementation should usually implement this interface.
Emphasize: What is recommended in JDK is to implement the RandomAccess interface for List collections as much as possible
If the collection class is an implementation of RandomAccess, try to traverse it with for(int i = 0; i < size; i++) instead of iterator iterator.
Conversely, if the List is a Sequence List, it is best to iterate with an iterator.
It is clear in the JDK that in the traversal algorithm of List, especially Huge size, we should try to determine whether it belongs to RandomAccess (such as ArrayList) or Sequence List (such as LinkedList). Because the traversal algorithm suitable for RandomAccess List is very different when used on Sequence List. The commonly used method is:
To make a judgment:
if (list instance of RandomAccess) { for(int m = 0; m < list.size(); m++){} }else{ Iterator iter = list.iterator(); while(iter.hasNext()){} }test:
public class TestRandomAccess { @Test public void testTraverse() { ArrayList<Integer> arraylist = new ArrayList<Integer>(); LinkedList<Integer> linkedList = new LinkedList<Integer>(); initList(arraylist, 1000); initList(linkedList, 1000); System.out.println("ArrayList implements RandomAccess interface"); implRandomAccessTraverse(arraylist); //It took 10ms to System.out.println("LinkedList does not implement RandomAccess interface"); implRandomAccessTraverse(linkedList); //It took 434ms to System.out.println("/nArrayList does not implement RandomAccess interface"); noImplRandomAccessTraverse(arraylist); //It took 39ms to System.out.println("LinkedList does not implement RandomAccess interface"); noImplRandomAccessTraverse(linkedList); //It took 27ms to} private long startTime = 0; private long endTime = 0; // Initialize the list public void initList(List<Integer> list, int n) { for (int i = 0; i < n; i++) { list.add(i); } } //There is a traversal of all data that implements the RandomAccess interface, public void implRandomAccessTraverse(List list) { startTime = System.currentTimeMillis(); for (int count = 0; count <= 1000; count++) { for (int i = 0; i < list.size(); i++) { list.get(i); } } endTime = System.currentTimeMillis(); System.out.println("It took a total of iteration using loop" + (endTime - startTime) + "ms time"); } //The traversal of all data without implementing the RandomAccess interface public void noImplRandomAccessTraverse(List list) { startTime = System.currentTimeMillis(); for (int count = 0; count <= 1000; count++) { for (Iterator itr = list.iterator(); itr.hasNext();) { itr.next(); } } endTime = System.currentTimeMillis(); System.out.println("It took a total of iterations using Iterator" + (endTime - startTime) + "ms time"); }}The above comprehensive understanding of the Java interface RandomAccess is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.