LinkedList
实现了 List
接口和 Deque
接口,是一个双端列表。LinkedList的底层链表结构使他支持高效的插入和删除操作,另外它实现了Deque接口,使得LinkedList也有队列的特性。
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
- LinkedList 没有实现
RandomAccess
接口,说明它不支持随机访问,因为其底层是链表
内部结构
LinkedList类的内部有一个静态私有类Node,Node中有两者指针,分别指向前后元素。LinkedList中还有两个指向头部和尾部的指针。
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;
}
}
源码分析
构造方法
// 空构造方法
public LinkedList() {
}
// 用已有的集合创建链表的构造方法
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
List接口继承的方法
add方法
add(E e)
将元素添加至链表尾部
public boolean add(E e) {
linkLast(e);
return true;
}
/**
* Links e as last element.
*/
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++;
}
add(int index, E element)
将元素添加至链表指定位置
public void add(int index, E element) {
checkPositionIndex(index); // 检查索引是否在0-size之间
if (index == size) //如果添加的位置正好是尾部
linkLast(element);
else
linkBefore(element, node(index)); // node(index) 找到位于index索引的节点
}
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
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;
}
}
addAll(Collection<? extends E> c)
将集合插入链表尾部
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
addAll(int index, Collection<? extends E> c)
将集合插入链表指定位置
public boolean addAll(int index, Collection<? extends E> c) {
// 检查index范围是否在0-size
checkPositionIndex(index);
// toArray把集合的数据存到对象数组中
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;
}
// 如果插入位置在尾部,重置last节点
if (succ == null) {
last = pred;
} else { //否则,将插入的链表与先前链表连接起来
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
E get(int index)
获取指定位置的值
public E get(int index) {
checkElementIndex(index); // 检查指定位置是否为0-size
return node(index).item;
}
Deque接口继承的方法
addFirst(E e)
将元素添加至链表头部
public void addFirst(E e) {
linkFirst(e);
}
addLast(E e)
将元素添加至链表尾部,同add(E e)
public void addLast(E e) {
linkLast(e);
}
offer(E e)
队列的入队方法,相当于从尾端添加元素
public boolean offer(E e) {
return add(e);
}
E poll()
队列的出队方法,相当于从头部删除元素
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
E poll()
队列的出队方法,相当于从头部删除元素
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
push(E e)
入栈方法,相当于从头部添加元素
public void push(E e) {
addFirst(e);
}
E pop()
出栈方法,相当于从头部删除元素
public E pop() {
return removeFirst();
}
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
E peek()
获取栈顶元素,相当于获取链表头部元素
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
方法那么多,该如何选
获取头节点的方法:
如果没有元素,get会触发异常,peek则会返回Null
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
删除头节点的方法:
如果没有元素,pop,remove会触发异常,poll会返回空。
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
public E pop() {
return removeFirst();
}
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
总结:
在Deque<> deque = new LinkedList();
将LinkedList当作双端队列时,最好使用含有队列语义的方法:offer
poll
peek
,这样就不会引发异常。