注:博主java集合框架源碼剖析系列的源碼所有基於JDK1.8.0版本。java
在實際項目中LinkedList也是使用頻率很是高的一種集合,本博客將從源碼角度帶領你們學習關於LinkedList的知識。node
一LinkedList類的定義:數組
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable從上述代碼能夠看到:LinkedList繼承自AbstractSequentialList同時實現了List,Deque,Cloneable與Serializable接口,因此LinkedList能夠看成雙端隊列使用。
二LinkedList類一些重要屬性:數據結構
transient int size = 0; transient Node<E> first;//指向第一個節點的指針 transient Node<E> last;//指向最後一個節點的指針
從這裏能夠看到LinkedList所有操做是基於Node數據結構的,而Node是LinkedList的一個內部類,本質上是一個雙向鏈表,即LinkedList底層是基於雙向鏈表實現的,所以LinkedList具有較好的插入,刪除的能力。Node類定義以下:框架
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內部實現原理:咱們先來看一下LinkedList的構造器
public LinkedList() { } public LinkedList(Collection<? extends E> c) { this(); addAll(c); }能夠看到在構造器重調用了addAll(c),咱們來看一下addAll的源碼:
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;//pred表示前驅節點,succ表示後繼節點 if (index == size) {// 若是插入位置爲鏈表末尾,則後繼爲null,前驅爲尾結點 succ = null; pred = last; } else { succ = node(index);//調用node()函數找到下標爲index的結點 pred = succ.prev;//保存該結點的前驅 } for (Object o : a) {//遍歷數組的同時將數組中的元素轉化爲Node類型插入到恰當位置 @SuppressWarnings("unchecked") E e = (E) o; Node<E> newNode = new Node<>(pred, e, null); if (pred == null)// 表示在第一個元素以前插入(索引爲0的結點) first = newNode; else pred.next = newNode; pred = newNode; } if (succ == null) {// 表示在最後一個元素以後插入 last = pred; } else { pred.next = succ; succ.prev = pred; } size += numNew; modCount++; return true; }addAll(index,c)該方法實現的功能便是在index位置處插入Collection集合c中所有的元素,能夠看到插入操做以前先將集合c轉換爲了數組結構,而後再將數組中的元素轉化爲Node節點插入的,同時能夠看到在該函數中調用了node函數,node函數源碼以下:
/** * Returns the (non-null) Node at the specified element index. */ Node<E> node(int index) { // assert isElementIndex(index); // 判斷插入的位置在鏈表前半段或者是後半段 if (index < (size >> 1)) {// 當index < size/2時,插入位置在前半段 Node<E> x = first; for (int i = 0; i < index; i++)// 從頭結點開始正向遍歷 x = x.next; return x; } else {// 插入位置在後半段 Node<E> x = last; for (int i = size - 1; i > index; i--)// 從尾結點開始反向遍歷 x = x.prev; return x; } }它的做用就是返回一個不爲null的節點的根據傳入的位置參數index,能夠看到該函數首先會經過index < (size >> 1)這個語句來判斷查詢的index位於前半段仍是後半段,結點在前半段則從頭開始遍歷,在後半段則從尾開始遍歷,這樣就保證了只須要遍歷最多一半結點就能夠找到指定索引的結點。提升了查找效率。
即當建立一個用集合參數初始化的LindedList的時候,LindedList的內部是先將該集合轉化爲數組,而後而後再將數組中的元素轉化爲Node節點插入從而構造出一個元素爲集合c的LinkedList。函數
四LinkedList一些重要的函數:學習
1. add函數this
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++; }
2get函數指針
public E get(int index) { checkElementIndex(index); return node(index).item; }能夠看到get函數其實是調用的node函數,即返回一個不爲null的節點的根據傳入的位置參數index。
3getFirst/getLastcode
public E getFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return f.item; } public E getLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return l.item; }
4remove函數
public boolean remove(Object o) { if (o == 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; }將元素o從集合LinkedList中刪除,其中調用到了unLink()函數,該函數用來將指定的結點從鏈表中斷開。
五:總結:
1LinkedList的內部是基於雙向循環鏈表實現的,雙向循環鏈表的定義爲Node,它是LinkedList的一個內部類。
2當用一個集合做爲參數來構造一個LinkedList時,其內部是是先將該集合轉化爲數組,而後而後再將數組中的元素轉化爲Node節點插入從而構造出一個元素爲集合c的LinkedList。
3能夠看到LinkedList中的方法都未使用synchronized關鍵字修飾,即LinkedList是非同步的,若是要建立一個同步的LinkedList則須要使用Collections.synchronizedList函數來進行構造:即List list = Collections.synchronizedList(new LinkedList(...));
3LinkedList實現了Deque,所以LinkedList能夠做爲雙端隊列使用,當須要使用隊列結構時,能夠考慮LinkedList。
4LinkedList容許重複元素存在,由於在add元素的過程當中不存在HashMap中put時判重替換的過程,只是進行簡單的插入操做。