鍍金池/ 教程/ Java/ LinkedList 的實(shí)現(xiàn)原理
LinkedHashSet 的實(shí)現(xiàn)原理
LinkedHashMap 與 LRUcache
HashSet 和 HashMap 的比較
Hashtable 的實(shí)現(xiàn)原理
ArrayList 的實(shí)現(xiàn)原理
HashSet 的實(shí)現(xiàn)原理
HashMap 的實(shí)現(xiàn)原理
LinkedList 的實(shí)現(xiàn)原理
ConcurrentHashMap 的實(shí)現(xiàn)原理
LinkedHashMap 的實(shí)現(xiàn)原理

LinkedList 的實(shí)現(xiàn)原理

概述

LinkedList 和 ArrayList 一樣,都實(shí)現(xiàn)了 List 接口,但其內(nèi)部的數(shù)據(jù)結(jié)構(gòu)有本質(zhì)的不同。LinkedList 是基于鏈表實(shí)現(xiàn)的(通過名字也能區(qū)分開來),所以它的插入和刪除操作比 ArrayList 更加高效。但也是由于其為基于鏈表的,所以隨機(jī)訪問的效率要比 ArrayList 差。

看一下 LinkedList 的類的定義:

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{}

LinkedList 繼承自 AbstractSequenceList,實(shí)現(xiàn)了 List、Deque、Cloneable、java.io.Serializable 接口。AbstractSequenceList 提供了List接口骨干性的實(shí)現(xiàn)以減少實(shí)現(xiàn) List 接口的復(fù)雜度,Deque 接口定義了雙端隊列的操作。

在 LinkedList 中除了本身自己的方法外,還提供了一些可以使其作為棧、隊列或者雙端隊列的方法。這些方法可能彼此之間只是名字不同,以使得這些名字在特定的環(huán)境中顯得更加合適。

LinkedList 也是 fail-fast 的(前邊提過很多次了)。

LinkedList 源碼解讀

數(shù)據(jù)結(jié)構(gòu)

LinkedList 是基于鏈表結(jié)構(gòu)實(shí)現(xiàn),所以在類中包含了 first 和 last 兩個指針(Node)。Node 中包含了上一個節(jié)點(diǎn)和下一個節(jié)點(diǎn)的引用,這樣就構(gòu)成了雙向的鏈表。每個 Node 只能知道自己的前一個節(jié)點(diǎn)和后一個節(jié)點(diǎn),但對于鏈表來說,這已經(jīng)足夠了。

    transient int size = 0;
    transient Node<E> first; //鏈表的頭指針
    transient Node<E> last; //尾指針
    //存儲對象的結(jié)構(gòu) Node, LinkedList的內(nèi)部類
    private static class Node<E> {
        E item;
        Node<E> next; // 指向下一個節(jié)點(diǎn)
        Node<E> prev; //指向上一個節(jié)點(diǎn)

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

存儲

add(E e)

該方法是在鏈表的 end 添加元素,其調(diào)用了自己的方法 linkLast(E e)。

該方法首先將 last 的 Node 引用指向了一個新的 Node(l),然后根據(jù)l新建了一個 newNode,其中的元素就為要添加的 e;而后,我們讓 last 指向了 newNode。接下來是自身進(jìn)行維護(hù)該鏈表。

/**
     * Appends the specified element to the end of this list.
     *
     * <p>This method is equivalent to {@link #addLast}.
     *
     * @param e element to be appended to this list
     * @return {@code true} (as specified by {@link Collection#add})
     */
public boolean add(E e) {
    linkLast(e);
    return true;
}
/**
* Links e as last element.
*/
void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}

add(int index, E element)

該方法是在指定 index 位置插入元素。如果 index 位置正好等于 size,則調(diào)用 linkLast(element) 將其插入末尾;否則調(diào)用 linkBefore(element, node(index))方法進(jìn)行插入。該方法的實(shí)現(xiàn)在下面,大家可以自己仔細(xì)的分析一下。(分析鏈表的時候最好能夠邊畫圖邊分析)

/**
    * Inserts the specified element at the specified position in this list.
    * Shifts the element currently at that position (if any) and any
    * subsequent elements to the right (adds one to their indices).
    *
    * @param index index at which the specified element is to be inserted
    * @param element element to be inserted
    * @throws IndexOutOfBoundsException {@inheritDoc}
    */
   public void add(int index, E element) {
       checkPositionIndex(index);

       if (index == size)
           linkLast(element);
       else
           linkBefore(element, node(index));
   }
   /**
        * Inserts element e before non-null Node succ.
        */
       void linkBefore(E e, Node<E> succ) {
           // assert succ != null;
           final Node<E> pred = succ.prev;
           final Node<E> newNode = new Node<>(pred, e, succ);
           succ.prev = newNode;
           if (pred == null)
               first = newNode;
           else
               pred.next = newNode;
           size++;
           modCount++;
       }

LinkedList 的方法實(shí)在是太多,在這沒法一一舉例分析。但很多方法其實(shí)都只是在調(diào)用別的方法而已,所以建議大家將其幾個最核心的添加的方法搞懂就可以了,比如 linkBefore、linkLast。其本質(zhì)也就是鏈表之間的刪除添加等。