本篇介紹的LinkedList是List接口的一種實現,它的底層是基於雙向鏈表實現的,因此它具有插入刪除快而查找修改慢的特點,此外,通過對雙向鏈表的操作還可以實現隊列和棧的功能。LinkedList的底層結構如下圖所示。
F表示頭結點引用,L表示尾結點引用,鏈表的每個結點都有三個元素,分別是前繼結點引用(P),結點元素的值(E),後繼結點的引用(N)。結點由內部類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;
}
}
Node這個內部類其實很簡單,只有三個成員變量和一個構造器,item表示結點的值,next爲下一個結點的引用,prev爲上一個結點的引用,通過構造器傳入這三個值。接下來再看看LinkedList的成員變量和構造器。
//集合元素個數
transient int size = 0;
//頭結點引用
transient Node<E> first;
//尾節點引用
transient Node<E> last;
//無參構造器
public LinkedList() {}
//傳入外部集合的構造器
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
LinkedList持有頭結點的引用和尾結點的引用,它有兩個構造器,一個是無參構造器,一個是傳入外部集合的構造器。與ArrayList不同的是LinkedList沒有指定初始大小的構造器。看看它的增刪改查方法。
//增(添加)
public boolean add(E e) {
//在鏈表尾部添加
linkLast(e);
return true;
}
//增(插入)
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size) {
//在鏈表尾部添加
linkLast(element);
} else {
//在鏈表中部插入
linkBefore(element, node(index));
}
}
//刪(給定下標)
public E remove(int index) {
//檢查下標是否合法
checkElementIndex(index);
return unlink(node(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;
}
//改
public E set(int index, E element) {
//檢查下標是否合法
checkElementIndex(index);
//獲取指定下標的結點引用
Node<E> x = node(index);
//獲取指定下標結點的值
E oldVal = x.item;
//將結點元素設置爲新的值
x.item = element;
//返回之前的值
return oldVal;
}
//查
public E get(int index) {
//檢查下標是否合法
checkElementIndex(index);
//返回指定下標的結點的值
return node(index).item;
}
LinkedList的添加元素的方法主要是調用linkLast和linkBefore兩個方法,linkLast方法是在鏈表後面鏈接一個元素,linkBefore方法是在鏈表中間插入一個元素。LinkedList的刪除方法通過調用unlink方法將某個元素從鏈表中移除。下面我們看看鏈表的插入和刪除操作的核心代碼。
//鏈接到指定結點之前
void linkBefore(E e, Node<E> succ) {
//獲取給定結點的上一個結點引用
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++;
}
//卸載指定結點
E unlink(Node<E> x) {
//獲取給定結點的元素
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;
}
linkBefore和unlink是具有代表性的鏈接結點和卸載結點的操作,其他的鏈接和卸載兩端結點的方法與此類似,所以我們重點介紹linkBefore和unlink方法。
linkBefore方法的過程圖:unlink方法的過程圖:
通過上面圖示看到對鏈表的插入和刪除操作的時間複雜度都是O(1),而對鏈表的查找和修改操作都需要遍歷鏈表進行元素的定位,這兩個操作都是調用的node(int index)方法定位元素,看看它是怎樣通過下標來定位元素的。
//根據指定位置獲取結點
Node<E> node(int 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;
}
}
通過下標定位時先判斷是在鏈表的上半部分還是下半部分,如果是在上半部分就從頭開始找起,如果是下半部分就從尾開始找起,因此通過下標的查找和修改操作的時間複雜度是O(n/2)。通過對雙向鏈表的操作還可以實現單項隊列,雙向隊列和棧的功能。
單向隊列操作:
//獲取頭結點
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
//獲取頭結點
public E element() {
return getFirst();
}
//彈出頭結點
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
//移除頭結點
public E remove() {
return removeFirst();
}
//在隊列尾部添加結點
public boolean offer(E e) {
return add(e);
}
雙向隊列操作:
//在頭部添加
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
//在尾部添加
public boolean offerLast(E e) {
addLast(e);
return true;
}
//獲取頭結點
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
//獲取尾結點
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
棧操作:
//入棧
public void push(E e) {
addFirst(e);
}
//出棧
public E pop() {
return removeFirst();
}
不管是單向隊列還是雙向隊列還是棧,其實都是對鏈表的頭結點和尾結點進行操作,它們的實現都是基於addFirst(),addLast(),removeFirst(),removeLast()這四個方法,它們的操作和linkBefore()和unlink()類似,只不過一個是對鏈表兩端操作,一個是對鏈表中間操作。可以說這四個方法都是linkBefore()和unlink()方法的特殊情況,因此不難理解它們的內部實現,在此不多做介紹。到這裏,我們對LinkedList的分析也即將結束,對全文中的重點做個總結:
1. LinkedList是基於雙向鏈表實現的,不論是增刪改查方法還是隊列和棧的實現,都可通過操作結點實現
2. LinkedList無需提前指定容量,因爲基於鏈表操作,集合的容量隨着元素的加入自動增加
3. LinkedList刪除元素後集合佔用的內存自動縮小,無需像ArrayList一樣調用trimToSize()方法
4. LinkedList的所有方法沒有進行同步,因此它也不是線程安全的,應該避免在多線程環境下使用
5. 以上分析基於JDK1.7,其他版本會有些出入,因此不能一概而論