Java 集合系列(三)—— LinkedList

以腦圖的形式來展現Java集合知識,讓零碎知識點造成體系php

LinkedList

   LinkedList是一種能夠在任何位置進行高效地插入和刪除操做的有序序列。
   它的最基本存儲結構是一個節點:每一個節點將存儲對象,以及先後節點的引用。html

結構圖

 
LinkedList 結構體

   從上面的結構圖中,咱們能夠了解到 ListedList 底層是基於雙向鏈表實現的。
   圍起來的能夠當作 LinkedList 類,它定義了三個 transient 成員變量:first、last、size。這三個變量是整個 LinkedList 類的關鍵點。node

  1. 因爲是雙向鏈表(每一個node都有保存先後節點的引用),所以咱們無論是由 first 仍是 last 節點開始迭代,均可以將整個鏈表的數據找出來;
  2. 在查詢、隨機插入以及set等操做都有涉及 size 判斷;
  3. 因爲 LinkedList 是雙向鏈表,類中只存儲了首尾兩個節點,所以查詢第n個元素都要從頭遍歷進行查找。

源碼分析

  add(E e)  源碼分析

 1     /**
 2      * Appends the specified element to the end of this list.
 3      *
 4      * <p>This method is equivalent to {@link #addLast}.
 5      *
 6      * @param e element to be appended to this list
 7      * @return {@code true} (as specified by {@link Collection#add})
 8      */
 9     public boolean add(E e) {
10         linkLast(e);
11         return true;
12     }
13     
14     /**
15      * Links e as last element.
16      */
17     void linkLast(E e) {
18         final Node<E> l = last;                             // 將當前最後一個元素寄存在 l
19         final Node<E> newNode = new Node<>(l, e, null);     // new 一個新節點:pre的引用爲l;存儲元素爲e;next的引用爲null
20         last = newNode;                                     // 將新節點引用覆蓋成員變量 last
21         if (l == null)                                      
22             first = newNode;                                // 若l爲null,說明以前鏈表爲空,此時新節點爲首個元素
23         else
24             l.next = newNode;                               // 不然,更新l的next引用
25         size++;                                             // size+1
26         modCount++;                                         // 非查詢操做 modCount 都會 +1
27     }

  add(int index, E element) 方法分析

 1     /**
 2      * Inserts the specified element at the specified position in this list.
 3      * Shifts the element currently at that position (if any) and any
 4      * subsequent elements to the right (adds one to their indices).
 5      *
 6      * @param index index at which the specified element is to be inserted
 7      * @param element element to be inserted
 8      * @throws IndexOutOfBoundsException {@inheritDoc}
 9      */
10     public void add(int index, E element) {
11         checkPositionIndex(index);  // 檢查 index 是否大於 size
12 
13         if (index == size)
14             linkLast(element);      // 直接在鏈表末尾追加
15         else
16             linkBefore(element, node(index));   // 插入index 節點前面
17     }
18     
19     
20     // 檢查 index 是否超出範圍 超出則拋出 IndexOutOfBoundsException
21     private void checkPositionIndex(int index) {
22         if (!isPositionIndex(index))
23             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
24     }
25 
26     /**
27      * Tells if the argument is the index of a valid position for an
28      * iterator or an add operation.
29      */
30     private boolean isPositionIndex(int index) {
31         return index >= 0 && index <= size;
32     }
33     
34     
35     
36     /**
37      * 根據 index 查找 node
38      * 該方法利用了雙向鏈表的特性,index 距離哪一個鏈表頭近就從哪邊開始開始遍歷
39      * 時間複雜度爲 O(n/2);
40      * 當 index 接近 size 的中間值時,效率最低
41      * Returns the (non-null) Node at the specified element index.
42      */
43     Node<E> node(int index) {
44         // assert isElementIndex(index);
45 
46         if (index < (size >> 1)) {          // size 右移一位(除以2)
47             Node<E> x = first;
48             for (int i = 0; i < index; i++)
49                 x = x.next;
50             return x;
51         } else {
52             Node<E> x = last;
53             for (int i = size - 1; i > index; i--)
54                 x = x.prev;
55             return x;
56         }
57     }

 

優缺點

優勢git

  • 增刪元素效率高(只須要更新節點附近的引用便可)

缺點github

  • 因爲查詢須要進行遍歷,所以效率低

 

知識腦圖

From Java Core Knowledge Treeapp

 
LinkedList 腦圖
 

  在 github 上建了一個 repository ,Java Core Knowledge Tree,各位看官如果喜歡請給個star,以示鼓勵,謝謝。
https://github.com/suifeng412/JCKTree源碼分析

 

(以上是本身的一些看法,如有不足或者錯誤的地方請各位指出)學習

 做者:那一葉隨風   http://www.cnblogs.com/phpstudy2015-6/ui

 原文地址: http://www.javashuo.com/article/p-eqovuejw-ca.html
this

 聲明:本博客文章爲原創,只表明本人在工做學習中某一時間內總結的觀點或結論。轉載時請在文章頁面明顯位置給出原文連接

相關文章
相關標籤/搜索