廢話很少說,先上測試結果。做者分別在ArrayList和LinkedList的頭部、尾部和中間三個位置插入與查找100000個元素所消耗的時間來進行對比測試,下面是測試結果
(感謝@Hosalo的指正,在這裏說明一下測試的環境,尾部插入是在空表的基礎上測試的,頭部和中間位置插入是在已存在100000個元素的表上進行測試的)java
插入 | 查找 | |
---|---|---|
ArrayList尾部 | 26ms | 4ms |
ArrayList頭部 | 2887ms | 3ms |
ArrayList中間 | 1936ms | 4ms |
LinkedList尾部 | 28ms | 9ms |
LinkedList頭部 | 15ms | 11ms |
LinkedList中間 | 12310ms | 11387ms |
咱們把Java中的ArrayList和LinkedList就是分別對順序表和雙向鏈表的一種實現,因此在進行源碼分析以前,咱們先來簡單回顧一下數據結構中的順序表與雙向鏈表中的關鍵概念node
因此咱們潛意識會認爲:ArrayList查找快,增刪慢。LinkedList查找慢,增刪快。但實際上真的是這樣的嗎?咱們一塊兒來看看吧。數組
測試程序代碼基本沒有什麼養分,這裏就不貼出來了,可是得把程序的運行結果貼出來,方便逐個分析。數據結構
ArrayList尾部插入100000個元素耗時:26ms
LinkedList尾部插入100000個元素耗時:28ms
ArrayList頭部插入100000個元素耗時:859ms
LinkedList頭部插入100000個元素耗時:15ms
ArrayList中間插入100000個元素耗時:1848ms
LinkedList中間插入100000個元素耗時:15981ms
ArrayList頭部讀取100000個元素耗時:7ms
LinkedList頭部讀取100000個元素耗時:11ms
ArrayList尾部讀取100000個元素耗時:12ms
LinkedList尾部讀取100000個元素耗時:9ms
ArrayList中間讀取100000個元素耗時:13ms
LinkedList中間讀取100000個元素耗時:11387ms
函數
add(E e)方法源碼分析
public boolean add(E e) {
// 檢查是否須要擴容
ensureCapacityInternal(size + 1); // Increments modCount!!
// 直接在尾部添加元素
elementData[size++] = e;
return true;
}
複製代碼
能夠看出,對ArrayList的尾部插入,直接插入便可,無須額外的操做。性能
LinkedList中定義了頭尾節點測試
/** * Pointer to first node. */
transient Node<E> first;
/** * Pointer to last node. */
transient Node<E> last;
複製代碼
add(E e)方法,該方法中調用了linkLast(E e)方法spa
public boolean add(E e) {
linkLast(e);
return true;
}
複製代碼
linkLast(E e)方法,能夠看出,在尾部插入的時候,並不須要從頭開始遍歷整個鏈表,由於已經事先保存了尾結點,因此能夠直接在尾結點後面插入元素指針
/** * Links e as last element. */
void linkLast(E e) {
// 先把原來的尾結點保存下來
final Node<E> l = last;
// 建立一個新的結點,其頭結點指向last
final Node<E> newNode = new Node<>(l, e, null);
// 尾結點置爲newNode
last = newNode;
if (l == null)
first = newNode;
else
// 修改原先的尾結點的尾結點,使其指向新的尾結點
l.next = newNode;
size++;
modCount++;
}
複製代碼
對於尾部插入而言,ArrayList與LinkedList的性能幾乎是一致的
add(int index, E element)方法,能夠看到經過調用系統的數組複製方法來實現了元素的移動。因此,插入的位置越靠前,須要移動的元素就會越多
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1); // Increments modCount!!
// 把原來數組中的index位置開始的元素所有複製到index+1開始的位置(其實就是index後面的元素向後移動一位)
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
// 插入元素
elementData[index] = element;
size++;
}
複製代碼
add(int index, E element)方法,該方法先判斷是不是在尾部插入,若是是調用linkLast()方法,不然調用linkBefore(),那麼是否真的就是須要重頭開始遍歷呢?咱們一塊兒來看看
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
複製代碼
在頭尾之外的位置插入元素固然得找出這個位置在哪裏,這裏面的node()方法就是關鍵所在,這個函數的做用就是根據索引查找元素,可是它會先判斷index的位置,若是index比size的一半(size >> 1,右移運算,至關於除以2)要小,就從頭開始遍歷。不然,從尾部開始遍歷。從而能夠知道,對於LinkedList來講,操做的元素的位置越往中間靠攏,效率就越低
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
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;
}
}
複製代碼
這個函數的工做就只是負責把元素插入到相應的位置而已,關鍵的工做在node()方法中已經完成了
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++;
}
複製代碼
第一次在掘金擼博客,但願之後多給你們分享一些有趣的知識!