以前別人面試我,這個問題的時候我一般都是回答:linkendlist增刪改塊,arraylist查找塊。直到最近我看了掘金的一篇博文,才發現,實踐出真知啊。
測試結果
分別在ArrayList和LinkedList的頭部、尾部和中間三個位置插入與查找100000個元素所消耗的時間來進行對比測試,下面是測試結果
List | 插入 | 查找 |
---|---|---|
ArrayList頭部 | 2859ms | 7ms |
ArrayList尾部 | 26ms | 12ms |
ArrayList中間 | 848ms | 13ms |
LinkedList頭部 | 15ms | 11ms |
LinkedList尾部 | 28ms | 11ms |
LinkedList中間 | 15981ms | 34928ms |
測試結論
- ArrayList的查找性能絕對是一流的,無論查詢的是哪個位置的元素
- ArrayList除了尾部插入的性能較好外(位置越靠後性能越好),其他位置性能就不如人意了
- LinkedList在頭尾查找頭尾性能都很棒,但是在中間位置進行操作的話,性能就差很遠了,而且跟ArrayList完全不是一個量級的,並且Linkedlist並不是插入哪裏性能都比Arraylist快,越靠中間,插入越慢。
源碼分析
我們把Java中的ArrayList和LinkedList就是分別對順序表和雙向鏈表的一種實現:
- 順序表:需要申請連續的內存空間保存元素,可以通過內存中的物理位置直接找到元素的邏輯位置。在順序表中間插入or刪除元素需要把該元素之後的所有元素向前or向後移動。
- 雙向鏈表:不需要申請連續的內存空間保存元素,需要通過元素的頭尾指針找到前繼與後繼元素(查找元素的時候需要從頭or尾開始遍歷整個鏈表,直到找到目標元素)。在雙向鏈表中插入or刪除元素不需要移動元素,只需要改變相關元素的頭尾指針即可。
所以我們潛意識會認爲:ArrayList查找快,增刪慢。LinkedList查找慢,增刪快;但實際上並不是這樣的。
ArrayList尾部插入
add(E e)方法
public boolean add(E e) {
// 檢查是否需要擴容
ensureCapacityInternal(size + 1); // Increments modCount!!
// 直接在尾部添加元素
elementData[size++] = e;
return true;
}
LinkedList尾部插入
LinkedList中定義了頭尾節點
/**
* Pointer to first node.
*/
transient Node<E> first;
/**
* Pointer to last node.
*/
transient Node<E> last;
add(E e)方法,該方法中調用了linkLast(E e)方法
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的性能幾乎是一致的
ArrayList頭部插入
add(int index, E element)方法,可以看到通過調用系統的數組複製方法來實現了元素的移動。所以,插入的位置越靠前,需要移動的元素就會越多
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1);
// 把原來數組中的index位置開始的元素全部複製到index+1開始的位置(其實就是index後面的元素向後移動一位)
System.arraycopy(elementData,index,elementData, index + 1,size - index);
// 插入元素
elementData[index] = element;
size++;
}
LinkedList頭部插入
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));
}
linkBefore方法
這個函數的工作就只是負責把元素插入到相應的位置而已,關鍵的工作在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++;
}
node方法
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()方法就是關鍵所在,這個函數的作用就是根據索引查找元素,但是它會先判斷index的位置,如果index比size的一半(size >> 1,右移運算,相當於除以2)要小,就從頭開始遍歷。否則,從尾部開始遍歷。從而可以知道,對於LinkedList來說,操作的元素的位置越往中間靠攏,效率就越低。
ArrayList、LinkedList查找
- 這就沒啥好說的了,對於ArrayList,無論什麼位置,都是直接通過索引定位到元素,時間複雜度O(1)
- 而對於LinkedList查找,其核心方法就是上面所說的node()方法,所以頭尾查找速度極快,越往中間靠攏效率越低
總結
- 對於LinkedList來說,頭部插入和尾部插入時間複雜度都是O(1)
- 但是對於ArrayList來說,頭部的每一次插入都需要移動size-1個元素,效率可想而知
- 但是如果都是在最中間的位置插入的話,ArrayList速度比LinkedList的速度快將近10倍