ArrayList, LinkedList, CopyOnWriteArrayList source code analysis records

ArrayList bottom is carried out based on dynamic array

1, List size is the length of the increment-decrement performed according deletions element

2, ArrayList when adding elements, first determine whether the length of the array is sufficient enough to put a new element when added to the array, if it is enough for expansion (i.e., replicate arrays), after the completion of adding the new expansion element to add a new array to complete the operation data;

3, ArrayList when you delete an element, also rely on the array are copied to the overall elements in the array to move forward one, the elements to be deleted overwritten, the last position on this array and empty out is set to null, so that GC will be recovered, bringing the delete operation is complete;

. 4, array replicate ArrayList carrying deletions when dependent change operation is performed, so when a large amount of data when the element array and the operation at the time of the end of the array, ArrayList high efficiency (because of the small number of elements to be copied );

5, System.arraycopy (source array, the source array subscript start position, the target length of the array, the array index of the starting position of the target, the source array to be copied);

6, Arrays.copyOf (source array, the length of the target array);

7, ArrayList implements RandomAccess interfaces (a marker interfaces, interface there is no way to achieve this through the data using ordinary for loop interface does not achieve this with an Iterator interface to traverse the data), higher query efficiency, but modify the data slower, suitable for ordinary for loop to iterate over its data, it is thread safe.

 

LinkedList bottom is based on a doubly linked list (pointer) to the
first element: | previous | element | next | ... _______________ ...

The second element: | previous | element | next |

previous: a reference to the element at the address on the
next: next address points to an element of the reference
element: the actual data is stored in
the first element a second element .next = .previous / second element first .next = elements .previous
LinkedList additions and deletions are carried out by modifying the list of previous and next, modification faster, but find elements slowly.
LinkedList suitable for use foreach to traverse the data, the higher the efficiency, the thread is unsafe.

 

ArrayList, the LinkedList differences :
ArrayList find that are high efficiency, but the low modifying element efficiency (involving the array copy)
the LinkedList find low element efficiency, but modified elements high efficiency
when ArrayList operation data when the end of the array, the operating efficiency of data is not sub in LinkedList; LinkedList operated when the data in the linked list forward position, the operation efficiency is higher than ArrayList.
ArrayList faster sequential insert

 

CopyOnWriteArrayList operation data ArrayList similar, but different, and ArrayList and
its add, delete, modify, insert the operating principle is the same, is a bottom Object [] array, then a new instance of the array;

When multi-threaded operation of this data, CopyOnWriteArrayList be able to ensure that the final consistency of the data (ie: separate read and write, eventually consistent, read the original array, a new array write operations, the final synchronization), are thread-safe.

Method to add an example:

Look at the definition of 3.1 CopyOnWriteArrayList

 public class CopyOnWriteArrayList<E>  implements List<E>, RandomAccess, Cloneable, java.io.Serializable {

private static final long serialVersionUID = 8673264195747942595L; transient final ReentrantLock lock = new ReentrantLock(); Private volatile transient Object [] Array; // bottom is this array ... } public a CopyOnWriteArrayList () { // points to this object when instantiating a new array of length 0 setArray ( new new Object [0 ]); } Final void setArray (Object [] A) { // the Object [] array references to newly created array Array = A; }

 

3.2 Look at the definition of the add method (CopyOnWriteArrayList can be analyzed to compare the performance of write-consuming operation because all you need to create a new array each time a new data)

public boolean add(E e) {
   final ReentrantLock lock = this.lock;
   Lock.lock ();     // to lock 
   the try {
      Object [] Elements = getArray (); // Get original array 
      int len = elements.length;
      Object [] the newElements = Arrays.copyOf (Elements, len +. 1);   // instantiate a new array (the expansion), and copy the original data array into a new array 
      the newElements [len] = E; // the add the new data to the new array setArray (the newElements); // the Object array reference to the array point to the newly created return true; 
    } the finally {
         lock.unlock (); // unlock   
    }
}

 

 

Disclaimer: This article is CSDN blogger original article "moon the stars', and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/weixin_43179996/article/details/87261368

 

Guess you like

Origin www.cnblogs.com/yrjns/p/12381775.html