LinkedList源碼解析

 一、簡介
LinkedList類聲明以下:
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
能夠發現 LinkedList繼承了 AbstractSequentialList抽象類,而不是像 ArrayList和 Vector那樣實現 AbstractList,實際上,java類庫中只有 LinkedList繼承了這個抽象類,正如其名,它提供了對序列的連續訪問的抽象:
LinkedList的底層是 Deque雙向鏈表,實現了 Deque接口,而 Deque接口繼承於 Queue接口,所以,在java中,若是要實現隊列,通常都使用 LinkedList來實現。
 
Node結點
Node節點 一共有三個屬性:item表明節點值,prev表明節點的前一個節點,next表明節點的後一個節點。
LinkedList中關於鏈表結點的定義以下:
private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;
        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }
每一個結點都有一個前驅和後繼結點,而且在 LinkedList中也定義了兩個變量分別指向鏈表中的第一個和最後一個結點:
transient Node<E> first;
transient Node<E> last;

 

二、添加(add)操做
public boolean add(E e) {
    linkLast(e);
    return true;
}

void linkLast(E e) {
    final Node<E> l = last;//鏈表最後一個節點
    final Node<E> newNode = new Node<>(l, e, null);//創建節點對象,前一個(perv屬性)是last,後一個(next屬性)是null,中間item數據是輸入的參數e
    last = newNode;
    if (l == null)
        first = newNode; //若是最後一個節點 爲null表示鏈表爲空,則添加數據時將新加的數據做爲第一個節點
    else
        l.next = newNode; //若是鏈表不爲空則將最後一個鏈表的next屬性指向新添加的節點
    size++; //鏈表長度自增1
    modCount++;
}

private static class Node<E> {
    E item;
    Node<E> next;
    Node<E> prev;

    Node(Node<E> prev, E element, Node<E> next) {
        this.item = element;
        this.next = next;
        this.prev = prev;
    }
}
總結:
LInkedList添加操做時每一個新添加的對象都會被放到新建的Node對象中,Node對象中包含加入的對象和先後指針屬性(pred和next,pred和next其實都是Node對象,直接存放的就是當前對象的前一個節點對象和後一個節點對象,最後一個及節點的next值爲null),而後將原來最後一個last節點的next指針指向新加入的對象,便可
 
addAll操做:
public boolean addAll(int index, Collection<? extends E> c) {
    checkPositionIndex(index);//判斷index是否越界,越界則拋出異常
    Object[] a = c.toArray();
    int numNew = a.length;//要插入的集合的長度
    if (numNew == 0)
        return false;
    Node<E> pred, succ;//聲明pred和succ兩個Node對象,用於標識要插入元素的前一個節點和最後一個節點
    if (index == size) { //若是size等於原數組長度則表示在結尾添加
        succ = null;
        pred = last;
    } else {
        succ = node(index);//index位置上的Node對象
        pred = succ.prev;
    }
    for (Object o : a) { //遍歷要插入的集合
        @SuppressWarnings("unchecked") E e = (E) o;
        Node<E> newNode = new Node<>(pred, e, null);
        if (pred == null)
            first = newNode; //若是要插入的位置的前一個節點爲null表示是第一個節點,則直接將newNode賦給第一個節點
        else
            pred.next = newNode; //將要插入的集合元素節點對象賦給此位置原節點對象的前一個對象的後一個,即更改前一個節點對象的next指針指到新插入的節點上
        pred = newNode;//更改指向後將新節點對象賦給pred做爲下次循環中新插入節點的前一個對象節點,依次循環
    }
    //此時pred表明集合元素的插入完後的最後一個節點對象
    if (succ == null) { //結尾添加的話在添加完集合元素後將最後一個集合的節點對象pred做爲last
        last = pred;
    } else {
        pred.next = succ;//將集合元素的最後一個節點對象的next指針指向原index位置上的Node對象
        succ.prev = pred;//將原index位置上的pred指針對象指向集合的最後一個對象
    }
    size += numNew;
    modCount++;
    return true;
}

Node<E> node(int index) {
    if (index < (size >> 1)) { //判斷index是否小於size的一半,若是小於則從頭遍歷節點,不然從結尾遍歷節點
        Node<E> x = first;
        for (int i = 0; i < index; i++)
            x = x.next; //從first第一個節點開始,依次將後一個節點賦給x
        return x; //返回index位置上的Node對象,下同理
    } else {
        Node<E> x = last;
        for (int i = size - 1; i > index; i--)
            x = x.prev;
        return x;
    }
}
總結:
LinkedList在某個位置插入元素是經過將原位置節點的前一個節點的後一個指針指向新插入的元素,而後將原位置的節點的前一個指針指向新元素來實現的,至關於插入元素後後面的元素後移了,可是不是像ArrayList那樣將全部插入位置後面的元素都後移,此處只是改變其先後節點的指向
 
三、刪除操做
public E remove(int index) {
    checkElementIndex(index);
    return unlink(node(index));
}

E unlink(Node<E> x) {
    final E element = x.item;
    final Node<E> next = x.next;
    final Node<E> prev = x.prev;
    if (prev == null) {
        first = next;
    } else {
        prev.next = next;//將傳入的節點的下一個節點對象賦給其以前前一個節點的下一個節點對象,即將傳入的節點對象跳過
        x.prev = null;//將傳入對象的前一個節點對象置空使其前一個指針不指向任何元素
    }
    if (next == null) {
        last = prev;//若是next爲null表示是最後一個元素,直接將pred節點賦給last
    } else {
        next.prev = prev;
        x.next = null;//將傳入節點的後一個節點置空,使其後一個節點指針不指向任何元素
    }
    x.item = null;//將傳入的節點對象上的對象置空,也就是整個Node對象中的屬性都爲空了,後面會被GC回收
    size--;
    modCount++;
    return element;
}
總結:
LinkedList刪除操做是經過將index位置上的前一個節點的next執行index位置的後一個節點,同時將後一個節點的pred指向前一個節點,而後將index位置上的Node節點對象屬性都置空來實現的,置空後的Node對象會被GC垃圾回收期回收掉。
 
四、修改操做
public E set(int index, E element) {
    checkElementIndex(index);//檢查索引越界狀況
    Node<E> x = node(index);//獲取index位置上的節點對象
    E oldVal = x.item;
    x.item = element;//將新插入的元素直接賦給此位置上節點對象的item屬性便可
    return oldVal;
}
總結:
LinkedList中的實際數據保存在節點對象的item屬性中
 
五、查詢操做
public E get(int index) {
    checkElementIndex(index);//檢查索引越界狀況
    return node(index).item;//返回該index位置上的節點對象的item屬性,即該位置上的實際值
}
總結:
插敘操做是經過node方法實現的,而node方法是經過先判斷index位置是在整個鏈表的前一半仍是後一半來遍歷前一半或者後一半元素來獲取index位置上的元素
 
六、LinkedList和ArrayList的大體區別:
一、ArrayList繼承於 AbstractList, LinkedList繼承於 AbstractSequentialList;
二、ArrayList基於數組, LinkedList基於雙向鏈表,對於隨機訪問, ArrayList比較佔優點,LinkedList插入、刪除元素比較快,若是隻要調整指針的指向那麼時間複雜度是O(1),可是若是針對特定位置須要遍歷時,時間複雜度是O(n),也就是LinkedList在隨機訪問元素的話比較慢;
三、LinkedList沒有實現本身的 Iterator,可是有 ListIterator和 DescendingIterator;
四、LinkedList須要更多的內存,由於 ArrayList的每一個索引的位置是實際的數據,而 LinkedList中的每一個節點中存儲的是實際的數據和先後節點的位置;
五、ArrayList 和 LinkedList都是非同步的集合。
六、和ArrayList同樣,LinkedList也是非線程安全的,只有在單線程下才可使用。爲了防止非同步訪問,能夠採用以下方式建立LinkedList:List list= Collections.synchronizedList(new LinkedList());
七、LinkedList基於雙向鏈表實現,元素均可覺得null。
相關文章
相關標籤/搜索