Arraylist and LinkedList similarities and differences

1. Is the guarantee thread safety: ArrayList and LinkedList are not synchronized, that is not thread-safe;
2. the underlying data structure: Arraylist bottom using the Object array; LinkedList bottom using a doubly linked list data structure (JDK1.6 prior to circular list, JDK1.7 cancel the cycle doubly linked list and note the difference between two-way circular list :); details can be read JDK1.7-LinkedList circular list optimization https://www.cnblogs.com/xingele0917/p/3696593 .html
3. insert and delete elements affect the position of the subject:

① ArrayList using storage array, so inserting and removing elements of the time complexity of the impact receiving element location. For example: When performing add (E e) of the method, the ArrayList default at the end of the specified element is added to this list, this time complexity is O (1). But if i want to insert and delete elements at the specified location, then (add (int index, E element )) on time complexity is O (ni). Since when performing the above operation in the first set after the i-th element and i (ni) elements have to perform bit manipulation backward / forward direction of a.
② LinkedList linked list memory, the insertion, deletion time complexity of the element is not affected by the position of the element, is approximately O (1) and the array is approximately O (n).
4. supports fast random access: LinkedList does not support efficient random element access, and support for ArrayList. Quick access to fast random access is via the object element is an element number (corresponding to the get (int index) method).
5. Memory footprint: wasted space ArrayList is mainly reflected in the list at the end of the list will reserve some space capacity, cost and space LinkedList is reflected in each of its elements need to consume more space than ArrayList (because to store the direct and immediate predecessor and successor data).

Supplement: RandomAccess Interface

public interface RandomAccess {
}

View source we found that in fact nothing RandomAccess interface definition. So, in my opinion, but the interface is a flag RandomAccess nothing. What identity? Logo implement this interface with random access capabilities.

In binarySearch () method, it is to determine whether the incoming list RamdomAccess instance, if it is, call indexedBinarySearch () method, if not, then call iteratorBinarySearch () method

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

ArrayList implements RandomAccess interface, and LinkedList not achieved. why? I think it is related to the structure and underlying data!
ArrayList array is the bottom, and the bottom is LinkedList list. An array of natural support random access, time complexity is O (1), so called fast random access. Need to traverse the list to a specific location to access the elements of a specific location, time complexity is O (n), it does not support fast random access.
ArrayList implements RandomAccess interface illustrated his fast random access. RandomAccess interface is just identity, not to say ArrayList implement the RandomAccess interface that has the fast random access function!

Here then summarize the list traversal mode selection:
implements list RandomAccess interface preference ordinary for loop, followed foreach,
list RandomAccess interface is not implemented, preference iterator traversal (foreach to traverse the bottom is by iterator implementation), large size data, do not use ordinary for loop

  

Guess you like

Origin www.cnblogs.com/zhangyaotong/p/11245192.html