天天看點

Java 集合深入了解(11):LinkedList #

今天心情魚肚白,來學學 LinkedList 吧!

日常開發中,儲存一組資料使用的最多的就是 ArrayList, 其次就是 LinkedList 了。

我們知道 ArrayList 是以數組實作的,周遊時很快,但是插入、删除時都需要移動後面的元素,效率略差些。

而LinkedList 是以連結清單實作的,插入、删除時隻需要改變前後兩個節點指針指向即可,省事不少。

今天來看下 LinkedList 源碼。

#

LinkedList 繼承結構

Java 集合深入了解(11):LinkedList #

LinkedList 繼承自 AbstractSequentialList 接口,同時了還實作了 Deque, Queue 接口。

LinkedList 雙向連結清單實作

Java 集合深入了解(11):LinkedList #

可以看到, LinkedList 的成員變量隻有三個:

  • 頭節點 first
  • 尾節點 last
  • 容量 size

節點是一個雙向節點: 

Java 集合深入了解(11):LinkedList #

用一副圖表示節點:

Java 集合深入了解(11):LinkedList #

LinkedList 的方法

1.關鍵的幾個内部方法(頭部添加删除,尾部添加删除,擷取指定節點,指定節點的添加删除)

//插入到頭部
private void linkFirst(E e) {
    //擷取頭節點
    final Node<E> f = first;
    //建立一個節點,尾部指向之前的 頭元素 first
    final Node<E> newNode = new Node<>(null, e, f);
    //first 指向建立的節點
    first = newNode;
    //如果之前是空連結清單,建立的節點 也是最後一個節點
    if (f == null)
        last = newNode;
    else
        //原來的第一個節點(現在的第二個)頭部指向建立的頭結點
        f.prev = newNode;
    size++;
    modCount++;
}

//插入到尾部
void linkLast(E e) {
    //擷取尾部節點
    final Node<E> l = last;
    //建立一個節點,頭部指向之前的 尾節點 last
    final Node<E> newNode = new Node<>(l, e, null);
    //last 指向建立的節點
    last = newNode;
    //如果之前是空連結清單, 建立的節點也是第一個節點
    if (l == null)
        first = newNode;
    else
        //原來的尾節點尾部指向建立的尾節點
        l.next = newNode;
    size++;
    modCount++;
}

//在 指定節點 前插入一個元素,這裡假設 指定節點不為 null
void linkBefore(E e, Node<E> succ) {
    // 擷取指定節點 succ 前面的一個節點
    final Node<E> pred = succ.prev;
    //建立一個節點,頭部指向 succ 前面的節點,尾部指向 succ 節點,資料為 e
    final Node<E> newNode = new Node<>(pred, e, succ);
    //讓 succ 節點頭部指向 建立的節點
    succ.prev = newNode;
    //如果 succ 前面的節點為空,說明 succ 就是第一個節點,那現在建立的節點就變成第一個節點了
    if (pred == null)
        first = newNode;
    else
        //如果前面有節點,讓前面的節點
        pred.next = newNode;
    size++;
    modCount++;
}

//删除頭節點并傳回該節點上的資料,假設不為 null
private E unlinkFirst(Node<E> f) {
    // 擷取資料,一會兒傳回
    final E element = f.item;
    //擷取頭節點後面一個節點
    final Node<E> next = f.next;
    //使頭節點上資料為空,尾部指向空
    f.item = null;
    f.next = null; // help GC
    //現在頭節點後邊的節點變成第一個了
    first = next;
    //如果頭節點後面的節點為 null,說明移除這個節點後,連結清單裡沒節點了
    if (next == null)
        last = null;
    else
        next.prev = null;
    size--;
    modCount++;
    return element;
}

//删除尾部節點并傳回,假設不為空
private E unlinkLast(Node<E> l) {

    final E element = l.item;
    //擷取倒數第二個節點
    final Node<E> prev = l.prev;
    //尾節點資料、尾指針置為空
    l.item = null;
    l.prev = null; // help GC
    //現在倒數第二變成倒數第一了
    last = prev;
    if (prev == null)
        first = null;
    else
        prev.next = null;
    size--;
    modCount++;
    return element;
}

//删除某個指定節點
E unlink(Node<E> x) {
    // 假設 x 不為空
    final E element = x.item;
    //擷取指定節點前面、後面的節點
    final Node<E> next = x.next;
    final Node<E> prev = x.prev;

    //如果前面沒有節點,說明 x 是第一個
    if (prev == null) {
        first = next;
    } else {
        //前面有節點,讓前面節點跨過 x 直接指向 x 後面的節點
        prev.next = next;
        x.prev = null;
    }

    //如果後面沒有節點,說 x 是最後一個節點
    if (next == null) {
        last = prev;
    } else {
        //後面有節點,讓後面的節點指向 x 前面的
        next.prev = prev;
        x.next = null;
    }

    x.item = null;
    size--;
    modCount++;
    return element;
}

//擷取指定位置的節點
Node<E> node(int index) {
    // 假設指定位置有元素

    //二分一下,如果小于 size 的一半,從頭開始周遊
    if (index < (size >> 1)) {
        Node<E> x = first;
        for (int i = 0; i < index; i++)
            x = x.next;
        return x;
    } else {
        //大于 size 一半,從尾部倒着周遊
        Node<E> x = last;
        for (int i = size - 1; i > index; i--)
            x = x.prev;
        return x;
    }
}
           

這些内部方法實作了對 連結清單節點的 基本修改操作,每次操作都隻要修改前後節點的指針,時間複雜度為 O(1)。

很多公開方法都是通過調用它們實作的。

2.公開的添加方法:

//普通的在尾部添加元素
public boolean add(E e) {
    linkLast(e);
    return true;
}

//在指定位置添加元素
public void add(int index, E element) {
    checkPositionIndex(index);
    //指定位置也有可能是在尾部
    if (index == size)
        linkLast(element);
    else
        linkBefore(element, node(index));
}

//添加一個集合的元素
public boolean addAll(Collection<? extends E> c) {
    return addAll(size, c);
}

public boolean addAll(int index, Collection<? extends E> c) {
    checkPositionIndex(index);

    //把 要添加的集合轉成一個 數組
    Object[] a = c.toArray();
    int numNew = a.length;
    if (numNew == 0)
        return false;

    //建立兩個節點,分别指向要插入位置前面和後面的節點
    Node<E> pred, succ;
    //要添加到尾部
    if (index == size) {
        succ = null;
        pred = last;
    } else {
        //要添加到中間, succ 指向 index 位置的節點,pred 指向它前一個
        succ = node(index);
        pred = succ.prev;
    }

    //周遊要添加内容的數組
    for (Object o : a) {
        @SuppressWarnings("unchecked") E e = (E) o;
        //建立新節點,頭指針指向 pred
        Node<E> newNode = new Node<>(pred, e, null);
        //如果 pred 為空,說明建立的這個是頭節點
        if (pred == null)
            first = newNode;
        else
            //pred 指向建立的節點
            pred.next = newNode;
        //pred 後移一位
        pred = newNode;
    }

    //添加完後需要修改尾指針 last
    if (succ == null) {
        //如果 succ 為空,說明要插入的位置就是尾部,現在 pred 已經到最後了
        last = pred;
    } else {
        //否則 pred 指向後面的元素
        pred.next = succ;
        succ.prev = pred;
    }

    //元素個數增加
    size += numNew;
    modCount++;
    return true;
}

//添加到頭部,時間複雜度為 O(1)
public void addFirst(E e) {
    linkFirst(e);
}

//添加到尾部,時間複雜度為 O(1)
public void addLast(E e) {
    linkLast(e);
}
           

繼承自雙端隊列的添加方法:

//入棧,其實就是在頭部添加元素
public void push(E e) {
    addFirst(e);
}

//安全的添加操作,在尾部添加
public boolean offer(E e) {
    return add(e);
}

//在頭部添加
public boolean offerFirst(E e) {
    addFirst(e);
    return true;
}

//尾部添加
public boolean offerLast(E e) {
    addLast(e);
    return true;
}
           

3.删除方法:

//删除頭部節點
public E remove() {
    return removeFirst();
}

//删除指定位置節點
public E remove(int index) {
    checkElementIndex(index);
    return unlink(node(index));
}

//删除包含指定元素的節點,這就得周遊了
public boolean remove(Object o) {
    if (o == null) {
        //周遊終止條件,不等于 null
        for (Node<E> x = first; x != null; x = x.next) {
            if (x.item == null) {
                unlink(x);
                return true;
            }
        }
    } else {
        for (Node<E> x = first; x != null; x = x.next) {
            if (o.equals(x.item)) {
                unlink(x);
                return true;
            }
        }
    }
    return false;
}

//删除頭部元素
public E removeFirst() {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
    return unlinkFirst(f);
}

//删除尾部元素
public E removeLast() {
    final Node<E> l = last;
    if (l == null)
        throw new NoSuchElementException();
    return unlinkLast(l);
}

//删除首次出現的指定元素,從頭周遊
public boolean removeFirstOccurrence(Object o) {
    return remove(o);
}

//删除最後一次出現的指定元素,倒過來周遊
public boolean removeLastOccurrence(Object o) {
    if (o == null) {
        for (Node<E> x = last; x != null; x = x.prev) {
            if (x.item == null) {
                unlink(x);
                return true;
            }
        }
    } else {
        for (Node<E> x = last; x != null; x = x.prev) {
            if (o.equals(x.item)) {
                unlink(x);
                return true;
            }
        }
    }
    return false;
}
           

繼承自雙端隊列的删除方法:

public E pop() {
    return removeFirst();
}

public E pollFirst() {
    final Node<E> f = first;
    return (f == null) ? null : unlinkFirst(f);
}

public E pollLast() {
    final Node<E> l = last;
    return (l == null) ? null : unlinkLast(l);
}
           

清除全部元素其實隻需要把首尾都置為 null, 這個連結清單就已經是空的,因為無法通路元素。 

但是為了避免浪費空間,需要把中間節點都置為 null:

public void clear() {
    for (Node<E> x = first; x != null; ) {
        Node<E> next = x.next;
        x.item = null;
        x.next = null;
        x.prev = null;
        x = next;
    }
    first = last = null;
    size = 0;
    modCount++;
}
           

3.公開的修改方法,隻有一個 set :

//set 很簡單,找到這個節點,替換資料就好了
public E set(int index, E element) {
    checkElementIndex(index);
    Node<E> x = node(index);
    E oldVal = x.item;
    x.item = element;
    return oldVal;
}
           

4.公開的查詢方法:

//挨個周遊,擷取第一次出現位置
public int indexOf(Object o) {
    int index = 0;
    if (o == null) {
        for (Node<E> x = first; x != null; x = x.next) {
            if (x.item == null)
                return index;
            index++;
        }
    } else {
        for (Node<E> x = first; x != null; x = x.next) {
            if (o.equals(x.item))
                return index;
            index++;
        }
    }
    return -1;
}

//倒着周遊,查詢最後一次出現的位置
public int lastIndexOf(Object o) {
    int index = size;
    if (o == null) {
        for (Node<E> x = last; x != null; x = x.prev) {
            index--;
            if (x.item == null)
                return index;
        }
    } else {
        for (Node<E> x = last; x != null; x = x.prev) {
            index--;
            if (o.equals(x.item))
                return index;
        }
    }
    return -1;
}

//是否包含指定元素
public boolean contains(Object o) {
    return indexOf(o) != -1;
}

//擷取指定位置的元素,需要周遊
public E get(int index) {
    checkElementIndex(index);
    return node(index).item;
}

//擷取第一個元素,很快
public E getFirst() {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
    return f.item;
}

//擷取第一個,同時删除它
public E poll() {
    final Node<E> f = first;
    return (f == null) ? null : unlinkFirst(f);
}

//也是擷取第一個,和 poll 不同的是不删除
public E peek() {
    final Node<E> f = first;
    return (f == null) ? null : f.item;
}

//長得一樣嘛
public E peekFirst() {
    final Node<E> f = first;
    return (f == null) ? null : f.item;
 }

//最後一個元素,也很快
public E getLast() {
    final Node<E> l = last;
    if (l == null)
        throw new NoSuchElementException();
    return l.item;
}

public E peekLast() {
    final Node<E> l = last;
    return (l == null) ? null : l.item;
}
           

關鍵方法介紹完了,接下來是内部實作的疊代器,需要注意的是 LinkedList 實作了一個倒序疊代器 DescendingIterator;還實作了一個 ListIterator ,名叫 ListItr

疊代器

1.DescendingIterator 倒序疊代器

//很簡單,就是遊标直接在 疊代器尾部,然後颠倒黑白,說是向後周遊,實際是向前周遊
private class DescendingIterator implements Iterator<E> {
    private final ListItr itr = new ListItr(size());
    public boolean hasNext() {
        return itr.hasPrevious();
    }
    public E next() {
        return itr.previous();
    }
    public void remove() {
        itr.remove();
    }
}
           

2. ListItr 操作基本都是調用的内部關鍵方法,沒什麼特别的

private class ListItr implements ListIterator<E> {
    private Node<E> lastReturned;
    private Node<E> next;
    private int nextIndex;
    private int expectedModCount = modCount;

    ListItr(int index) {
        // 二分周遊,指定遊标位置
        next = (index == size) ? null : node(index);
        nextIndex = index;
    }

    public boolean hasNext() {
        return nextIndex < size;
    }

    public E next() {
        checkForComodification();
        if (!hasNext())
            throw new NoSuchElementException();
        //很簡單,後移一位
        lastReturned = next;
        next = next.next;
        nextIndex++;
        return lastReturned.item;
    }

    public boolean hasPrevious() {
        return nextIndex > 0;
    }

    public E previous() {
        checkForComodification();
        if (!hasPrevious())
            throw new NoSuchElementException();

        lastReturned = next = (next == null) ? last : next.prev;
        nextIndex--;
        return lastReturned.item;
    }

    public int nextIndex() {
        return nextIndex;
    }

    public int previousIndex() {
        return nextIndex - 1;
    }

    public void remove() {
        checkForComodification();
        if (lastReturned == null)
            throw new IllegalStateException();

        Node<E> lastNext = lastReturned.next;
        unlink(lastReturned);
        if (next == lastReturned)
            next = lastNext;
        else
            nextIndex--;
        lastReturned = null;
        expectedModCount++;
    }

    public void set(E e) {
        if (lastReturned == null)
            throw new IllegalStateException();
        checkForComodification();
        lastReturned.item = e;
    }

    public void add(E e) {
        checkForComodification();
        lastReturned = null;
        if (next == null)
            linkLast(e);
        else
            linkBefore(e, next);
        nextIndex++;
        expectedModCount++;
    }

    public void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (modCount == expectedModCount && nextIndex < size) {
            action.accept(next.item);
            lastReturned = next;
            next = next.next;
            nextIndex++;
        }
        checkForComodification();
    }

    final void checkForComodification() {
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
    }
}
           

還有個 LLSpliterator 繼承自 Spliterator, JDK 8 出來的新東東,這裡暫不研究

Spliterator 是 Java 8 引入的新接口,顧名思義,Spliterator 可以了解為 Iterator 的 Split 版本(但用途要豐富很多)。

使用 Iterator 的時候,我們可以順序地周遊容器中的元素,使用 Spliterator 的時候,我們可以将元素分割成多份,分别交于不于的線程去周遊,以提高效率。

使用 Spliterator 每次可以處理某個元素集合中的一個元素 — 不是從 Spliterator 中擷取元素,而是使用 tryAdvance() 或 forEachRemaining() 方法對元素應用操作。

但 Spliterator 還可以用于估計其中儲存的元素數量,而且還可以像細胞分裂一樣變為一分為二。這些新增加的能力讓流并行處理代碼可以很友善地将工作分布到多個可用線程上完成。

轉自 http://blog.sina.com.cn/s/blog_3fe961ae0102wxdb.html

總結

吐個槽,估計是很多人維護的,有些方法功能代碼完全一樣

比如:

private boolean isElementIndex(int index) {
    return index >= 0 && index < size;
}

private boolean isPositionIndex(int index) {
    return index >= 0 && index <= size;
}
           

LinkedList 特點

  • 雙向連結清單實作
  • 元素時有序的,輸出順序與輸入順序一緻
  • 允許元素為 null
  • 所有指定位置的操作都是從頭開始周遊進行的
  • 和 ArrayList 一樣,不是同步容器

并發通路注意事項

linkedList 和 ArrayList 一樣,不是同步容器。是以需要外部做同步操作,或者直接用 

Collections.synchronizedList

 方法包一下,最好在建立時就報一下:

List list = Collections.synchronizedList(new LinkedList(...));
           

LinkedList 的疊代器都是 fail-fast 的: 如果在并發環境下,其他線程使用疊代器以外的方法修改資料,會導緻 ConcurrentModificationException.

ArrayList VS LinkedList

Java 集合深入了解(11):LinkedList #

ArrayList

  • 基于數組,在數組中搜尋和讀取資料是很快的。是以 ArrayList 擷取資料的時間複雜度是O(1);
  • 但是添加、删除時該元素後面的所有元素都要移動,是以添加/删除資料效率不高;
  • 另外其實還是有容量的,每次達到門檻值需要擴容,這個操作比較影響效率。

LinkedList

  • 基于雙端連結清單,添加/删除元素隻會影響周圍的兩個節點,開銷很低;
  • 隻能順序周遊,無法按照索引獲得元素,是以查詢效率不高;
  • 沒有固定容量,不需要擴容;
  • 需要更多的記憶體,如文章開頭圖檔所示 LinkedList 每個節點中需要多存儲前後節點的資訊,占用空間更多些。

拓展

兩個隊列實作一個棧 ?

http://www.cnblogs.com/kaituorensheng/archive/2013/03/02/2939690.html

Thanks

http://www.kutear.com/post/java/2016-08-16-think_in_java_11_and_17

https://segmentfault.com/a/1190000002516799

http://blog.csdn.net/mynameishuangshuai/article/category/6438276

http://blog.csdn.net/u011518120/article/details/51984405

http://blog.csdn.net/u010370082/article/details/45046755

http://www.lai18.com/content/1257052.html