JDK1.8 in LinkedList the realization of the principle and source code analysis

See: https: //blog.csdn.net/cb_lcl/article/details/81222394

I. Overview
           LinkedList is based on the underlying doubly linked list (doubly linked list of features, you can look at my other blog post: https: //blog.csdn.net/cb_lcl/article/details/81217972), the list in memory is not continuous but by reference to all of the associated elements, there is an advantage that the linked list to add and delete elements faster because only movement of the pointer, and determines whether or not required capacity is needed, the disadvantage is the relatively low efficiency and traverse the query.

Second, source code analysis

2.1 class structure

**
 * LinkedList bottom is double-linked list.
 * Implements List interface can have a queue operation
 * Implements the interface Deque operation can deque
 * List implements all optional and allows storage operation of any element, including Null
 
 * All operations to cash a doubly-linked list structure.
 * List index into operation from the beginning or end of the traverse List, whatever a specified index
 *
 * Note: These implementations (linkedList) is not synchronized, meaning thread safe
 * If there are multiple threads access a doubly linked list, there is at least one thread list modification in the structure, then you must add the external synchronization (synchronized) (the so-called structural modifications
 * Operation means to add or modify one or more elements, reset the value is not modified structural elements), typically naturally encapsulated by the synchronization to complete some objects List
 *
 * If no such object exists, it should be used to encapsulate Collections.synchronizedList list.
 * Best in creation is complete, to access the list unexpected visit asynchronously.
 *如:List list = Collections.synchronizedList(new LinkedList(...));
 *
 * Iterators and iterative methods of this class return an iterator is fail-fast: if the list at any time after the iterator is created structured modify, add or remove except by methods of operation,
 * ConcurrentModificationException otherwise it will throw an exception, therefore, faced with high concurrent modification, the iterator after the failure of fast and clean to prevent bear
 * Risking any undetermined, non-deterministic-risk sexual behavior
 *
 * Note: iterators are fail-fast behavior can not guarantee that, in general, can not ensure that any commitment in the presence of concurrent modification, the iterator fails quickly
 * ConcurrentModificationException do our best to throw an exception, therefore, rely on this to write a program is wrong.
 * Abnormal correctness: the fail-fast behavior of iterators should be used only to detect errors
 *
 * @author cb
 *
 * @param <E>
 */
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable

2.2 member variables and constructor

/**
     * The current number of elements stored
     */
transient int size = 0;
 
/**
 * 
 * The first node
 */
transient Node<E> first;
 
/**
 * End node
 */
transient Node<E> last;
 
/**
 * Empty constructor
 */
public LinkedList() {
}
 
/**
 * Set of parameters passed in the constructor
 * / 
Public the LinkedList (Collection <? The extends E> C) {
     the this (); // call the constructor of the current class 
    addAll (c);
}

LinkedList two constructors can be seen from the above, a free parameter, a parameter, a functional constructor parameter is set by a parameter, and all the elements of the set which is inserted into the LinkedList, note that this is "Insert "instead of saying" initialization added, "because LinkedList is not thread-safe, it may be in this () after the call, there have been other threads to insert data inside.

Disclaimer: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/lukelook/p/11091825.html