java 源碼閱讀(三)LinkedList
LinkedList底層採用的是雙向鏈表結構,支持空值和重複值。無法向ArrayList那樣進行擴容,存儲元素時,需要額外的空間存儲前驅和後繼的引用。LinkedList在鏈表頭部和尾部的插入效率比較高,但在指定位置進行插入時,效率一般。操作複雜度爲O(N)。LinkedList是非線程安全的集合類。
繼承/實現
構造函數
transient int size = 0;
transient Node<E> first;
transient Node<E> last;
//內部類,雙向鏈表
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()
public LinkedList() {
}
LinkedList(Collection<? extends E>)
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
方法源碼解讀
get()
public E get(int index) {
//用來驗證當前index是否在size內
checkElementIndex(index);
//返回結果
return node(index).item;
}
private void checkElementIndex(int index) {
if (!isElementIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
Node<E> node(int index) {
// assert isElementIndex(index);
//這裏是二分查找。
if (index < (size >> 1)) {
//通過頭結點,一直遍歷後繼結點到index位置。
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
//通過尾結點,一直遍歷前驅結點到index位置。
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
add()
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
//將最後一個結點賦給l
final Node<E> l = last;
//新建一個Node,它的前驅結點是l
final Node<E> newNode = new Node<>(l, e, null);
//將newNode的值賦給尾結點
last = newNode;
if (l == null)
//如果l爲空,表示這是一個空的linkedList,將newNode賦給頭結點
first = newNode;
else
//否則,將l的後繼結點賦值爲newNode
l.next = newNode;
size++;
modCount++;
}
add(int,E)
public void add(int index, E element) {
//檢驗index是否在size內。
checkPositionIndex(index);
if (index == size)
//如果index==size,直接從尾部進行添加
linkLast(element);
else
//從index位置開始添加
linkBefore(element, node(index));
}
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
//將succ的前驅結點賦值給pred
final Node<E> pred = succ.prev;
//定義一個新的結點,它的前驅結點是pred,後繼結點是succ
final Node<E> newNode = new Node<>(pred, e, succ);
//將newNode賦值給succ的前驅結點
succ.prev = newNode;
//驗證當前鏈表是否爲空
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
remove()
public E remove() {
return removeFirst();
}
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
final E element = f.item;
final Node<E> next = f.next;
f.item = null;
f.next = null; // help GC
first = next;
if (next == null)
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}
代碼比較簡單,因此沒有加註釋。默認刪除頭結點,將第二個結點置爲頭結點。
remove(int)
public E remove(int index) {
//校驗index
checkElementIndex(index);
return unlink(node(index));
}
E unlink(Node<E> x) {
// assert x != null;
//將刪除的結點,後繼結點,前驅結點取出,
final E element = x.item;
final Node<E> next = x.next;
final Node<E> prev = x.prev;
//判斷是否是頭結點,修改後繼結點
if (prev == null) {
first = next;
} else {
prev.next = next;
x.prev = null;
}
//判斷是否是尾結點,修改前驅結點
if (next == null) {
last = prev;
} else {
next.prev = prev;
x.next = null;
}
x.item = null;
size--;
modCount++;
return element;
}
remove(Ojbect)
根據Object找到對應的index,然後進行刪除。
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;
}