前言
本篇作為吃透java集合系列第四篇,主要來了解一下LinkedList,通過本篇你将了解到如下:
1、LinkedList的List特性
2、LinkedList的Queue特性
一:LinkedList
LinkedList類是List接口的實作類,它是一個集合,可以根據索引來随機的通路集合中的元素,還實作了Deque接口,它還是一個隊列,可以當成雙端隊列來使用。
雖然LinkedList是一個List集合,但是它的實作方式和ArrayList是完全不同的,ArrayList的底層是通過一個動态的Object[]數組實作的,而LinkedList的底層是通過雙向連結清單來實作的,是以它的随機通路速度是比較差的,但是它的删除,插入操作很快。
- LinkedList是基于雙向循環連結清單實作的,除了可以當作連結清單操作外,它還可以當作棧、隊列和雙端隊列來使用。
- LinkedList同樣是非線程安全的,隻在單線程下适合使用。
- LinkedList允許null插入。
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
- 實作List接口,具有List集合的特性。
- 實作Deque接口,具有隊列的特性。
- 實作Cloneable接口,可以通過clone來實作淺拷貝
- 實作Serializable接口,可以序列化,通過writeObject和readObject自定義序列化。
LinkedList的底層結構如下圖所示
F表示頭結點引用,L表示尾結點引用,連結清單的每個結點都有三個元素,分别是前繼結點引用§,結點元素的值(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沒有指定初始大小的構造器。
二:LinkedList的List特性
//增
/**
* 将指定的元素追加到此清單的末尾
*/
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 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;
if (index == size) {
succ = null;
pred = last;
} else {
succ = node(index);
pred = succ.prev;
}
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)
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;
}
/**
* 頭插入
*/
public void addFirst(E e) {
linkFirst(e);
}
/**
* 尾插入
*/
public void addLast(E e) {
linkLast(e);
}
//删
/**
* 從清單中删除指定元素的第一個出現(如果存在)。
*/
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 remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
/**
* 從此清單中删除并傳回第一個元素。
*/
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
/**
* 從此清單中删除并傳回最後一個元素。
*/
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
//改
/**
* 用指定的元素替換此清單中指定位置的元素。
*/
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方法将某個元素從連結清單中移除。下面我們看看連結清單的插入和删除操作的核心代碼。
/**
* 傳回指定位置的節點
*/
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;
}
}
/**
* 連接配接到第一個元素
*/
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
if (f == null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
/**
* 連結到最後一個元素
*/
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++;
}
/**
* 在succ前插入元素e
*/
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++;
}
/**
* 去掉頭結點
*/
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;
}
/**
* 去掉尾結點
*/
private E unlinkLast(Node<E> l) {
// assert l == last && l != null;
final E element = l.item;
final Node<E> prev = l.prev;
l.item = null;
l.prev = null; // help GC
last = prev;
if (prev == null)
first = null;
else
prev.next = null;
size--;
modCount++;
return element;
}
/**
* 去掉指定的節點
*/
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;
}
三:LinkedList的Queue特性
通過對雙向連結清單的操作還可以實作單項隊列,雙向隊列和棧的功能。
單向隊列操作:
//擷取頭結點
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()這四個方法。
- LinkedList是基于雙向連結清單實作的,不論是增删改查方法還是隊列和棧的實作,都可通過操作結點實作
- LinkedList無需提前指定容量,因為基于連結清單操作,集合的容量随着元素的加入自動增加
- LinkedList删除元素後集合占用的記憶體自動縮小,無需像ArrayList一樣調用trimToSize()方法
- LinkedList的所有方法沒有進行同步,是以它也不是線程安全的,應該避免在多線程環境下使用