简体   繁体   中英

How does the List has fast random access in Java?

The RandomAccess interface identifies that a particular java.util.List implementation has fast random access. This interface tries to define an imprecise concept: how fast is fast? The documentation provides a simple guide:

if repeated access using the List.get() method is faster than repeated access
using the Iterator.next() method, then the List has fast random access. 

The two types of access are shown in the following code examples:

Object o;
for (int i=0, n=list.size(); i < n; i++)
o = list.get(i);

Object o;
for (Iterator itr=list.iterator(); itr.hasNext(); )
o = itr.next();

How does the List has fast random access when its list.get() method is faster than repeated access using the Iterator.next() method.

The imprecise nature of what it means to be "fast" is the reason we have the concept of algorithmic complexity, aka "big O" notation. Random access implies that get operations have an algorithmic complexity of O(1), which is true of ArrayList . By contrast, a get operation on a LinkedList has an algorithmic complexity of O(n).

How does the List has fast random access when its list.get() method is faster than repeated access using the Iterator.next() method.

It's only a suggestion that it's marked as having fast random access with the RandomAccess if the speed of list.get() is faster than the iterator.next(). The programmer programming the specific List implementation still has to choose to have their List implement RandomAccess.

It's understandable that since list.get(i) would directly query the data that such a method call would be faster than iterator.next() (for the List's that have good random access) because the next method may have some extra overhead of other method calls and other logic. If the iterator is faster, than you can probably safely bet it's because the List does not have good random access and that the iterator is faster because it's using some optimized method of going from node to node (such a List's iterator implementation probably has some access to underlying List data that would normally be private to other classes).

The example code is indeed a bit misleading, because the comparison to an iterator-based solution detracts from the actual point:

A list should implement the RandomAccess interface when the List#get(int) method has a constant asymptotic running time! (Written as O(1) in Big O Notation ).

Consider an ArrayList : The data internally stored in an array. Calling arrayList.get(1) will take the same time as calling arrayList.get(100000) .

In contrast to that: A LinkedList does not offer RandomAccess . While you can still call linkedList.get(1) and linkedList.get(100000) , the latter will take much longer, because it has to traverse the whole list until it reaches the 100000th element.

There is a way to implement - on linked lists - an O(log n) (worst case) random access. The method has no correlation with Skip-list, Okasaki's list and Haskell's trees. It has been presented HERE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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