文章目录
一、概述
LinkedList是一个实现了List接口和Deque接口的双端链表。
有关索引的操作可能从链表头开始遍历到链表尾部,也可能从尾部遍历到链表头部,这取决于看索引更靠近哪一端。
LinkedList不是线程安全的,如果想使LinkedList变成线程安全的,可以使用如下方式:
List list=Collections.synchronizedList(new LinkedList(...));
iterator()和listIterator()返回的迭代器都遵循fail-fast机制。
继承关系
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
内部结构
LinkedList内部是一个双端链表的结构,结构如下图:
二、成员变量
// list中的元素个数
transient int size = 0;
// 链表的头节点
transient Node<E> first;
// 链表的尾节点
transient Node<E> last;
下面是Node节点的定义,Node类LinkedList的静态内部类。
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() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);//添加集合中所有元素
}
四、添加操作
因为LinkedList即实现了List接口,又实现了Deque接口,所以LinkedList既可以添加将元素添加到尾部,也可以将元素添加到指定索引位置,还可以添加添加整个集合;另外既可以在头部添加,又可以在尾部添加。
add(E e)
public boolean add(E e) {
linkLast(e);
return true;
}
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 e)
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
node(int index)方法,该方法返回指定位置的节点
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;
}
}
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++;
}
addAll方法
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;
}
//如果插入位置在尾部,重置last节点
if (succ == null) {
last = pred;
} else { //否则,将插入的链表与先前链表连接起来
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
从上面的代码可以看到,addAll方法主要分为4步:
- 检查index索引范围
- 得到集合数据
- 得到插入位置的前驱和后继节点
- 遍历数据,将数据插入到指定位置
addFirst(E e)方法
public void addFirst(E e) {
linkFirst(e);
}
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++;
}
addLast(E e)方法
public void addLast(E e) {
linkLast(e);
}
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++;
}
offer(E e)方法
public boolean offer(E e) {
return add(e);
}
public boolean add(E e) {
linkLast(e);
return true;
}
offerFirst(E e)方法
offerFirst()方法用于将数据插入链表头部,与addFirst的区别在于该方法可以返回特定的返回值,而addFirst的返回值为void
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
offerLast(E e)方法
public boolean offerLast(E e) {
addLast(e);
return true;
}
添加操作总结
LinkedList由于实现了List和Deque接口,所以有多种添加方法,下面总结了一下。
- 将数据插入到链表尾部
- boolean add(E e):
- void addLast(E e)
- boolean offerLast(E e)
- 将数据插入到链表头部
- void addFirst(E e)
- boolean offerFirst(E e)
- 将数据插入到指定索引位置
- boolean add(int index,E e)
五、检索
get(int index)方法
根据指定索引返回数据,如果索引越界,那么会抛出异常。实现如下:
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
获得位置为0的头节点数据
LinkedList中有多种方法可以获得头节点的数据,实现大同小异,区别在于对链表为空时的处理,是抛出异常还是返回null。主要方法有getFirst()、element()、peek()、peekFirst()、方法。其中getFirst()和element()方法将会在链表为空时,抛出异常,它们的实现如下:
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
public E element() {
return getFirst();
}
从代码可以看到,element()方法的内部就是使用getFirst()实现的。它们会在链表为空时,抛出NoSuchElementException。下面再看peek()和peekFirst()的实现:
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;
}
获得位置为size-1的尾节点数据
获得尾节点数据的方法有getLast()和peekLast()。getLast()的实现如下:
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
可以看到,getLast()方法在链表为空时,会抛出NoSuchElementException,而peekLast()则不会,只是会返回null。实现如下:
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
根据对象得到索引
根据对象得到索引分为两种,一种是第一个匹配的索引,一个是最后一个匹配的索引,实现的在于一个从前往后遍历,一个从后往前遍历。下面先看idnexOf()方法的实现:
public int indexOf(Object o) {
int index = 0;
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null)
return index;
index++;
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item))
return index;
index++;
}
}
return -1;
}
从上面的代码可以看到,LinkedList可以包含null元素,遍历方式都是从前往后,一旦匹配了,就返回索引。
lastIndexOf()方法返回最后一个匹配的索引,实现为从后往前遍历,源码如下:
public int lastIndexOf(Object o) {
int index = size;
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (x.item == null)
return index;
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (o.equals(x.item))
return index;
}
}
return -1;
}
检查链表是否包含某对象
contains(Object o)方法检查对象o是否存在于链表中,其实现如下:
public boolean contains(Object o) {
return indexOf(o) != -1;
}
检索操作总结
检索操作分为按照位置得到对象以及按照对象得到位置两种方式,其中按照对象得到位置的方法有indexOf()和lastIndexOf();按照位置得到对象有如下方法:
- 根据任意位置得到数据的get(int index)方法,当index越界会抛出异常
- 获得头节点数据
- getFirst()和element()方法在链表为空时会抛出NoSuchElementException
- peek()和peekFirst()方法在链表为空时会返回null
- 获得尾节点数据
- getLast()在链表为空时会抛出NoSuchElementException
- peekLast()在链表为空时会返回null
六、删除操作
删除操作分为按照位置删除和按照对象删除,其中按照位置删除的方法又有区别,有的只是返回是否删除成功的标志,有的还需要返回被删除的元素。下面分别讨论。
删除指定对象
当删除指定对象时,只需调用remove(Object o)即可,不过该方法一次只会删除一个匹配的对象,如果删除了匹配对象,返回true,否则false。其实现如下:
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;
}
从代码可以看到,由于LinkedList可以存储null元素,所以对删除对象以是否为null做区分。然后从链表头开始遍历,一旦匹配,就会调用unlink()方法将该节点从链表中移除。下面是unlink()方法的实现:
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;
}
按照位置删除对象
boolean remove(int index)方法用于删除任意位置的元素,如果删除成功将返回true,否则返回false。实现如下:
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
删除头节点的对象
删除头节点的对象的方法有很多,包括remove()、removeFirst()、pop()、poll()、pollFirst(),其中前三个方法在链表为空时将抛出NoSuchElementException,后两个方法在链表为空时将返回null。
remove()、pop()、removeFirst()的实现如下:
public E remove() {
return removeFirst();
}
public E pop() {
return removeFirst();
}
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
从上面代码可以看到,remove()和pop()内部调用了removeFirst()方法,而removeFirst()在链表为空时将抛出NoSuchElementException。
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
可以看到poll()和pollFirst()的实现代码是相同的,在链表为空时将返回null。
删除尾节点的对象
删除尾节点的对象的方法有removeLast()和pollLast()。removeLast的实现如下:
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
可以看到pollLast()在链表为空时会返回null,而不是抛出异常。
删除操作总结
删除操作由很多种方法,有:
- 按照指定对象删除:boolean remove(Object o),一次只会删除一个匹配的对象
- 按照指定位置删除
- 删除任意位置的对象:E remove(int index),当index越界时会抛出异常
- 删除头节点位置的对象
- 在链表为空时抛出异常:E remove()、E removeFirst()、E pop()
- 在链表为空时返回null:E poll()、E pollFirst()
- 删除尾节点位置的对象
- 在链表为空时抛出异常:E removeLast()
- 在链表为空时返回null:E pollLast()
迭代器操作
LinkedList的iterator()方法内部调用了其listIterator()方法,所以可以只分析listIterator()方法。listIterator()提供了两个重载方法。iterator()方法和listIterator()方法的关系如下:
public Iterator<E> iterator() {
return listIterator();
}
public ListIterator<E> listIterator() {
return listIterator(0);
}
public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
return new ListItr(index);
}
从上面可以看到三者的关系是iterator()——>listIterator(0)——>listIterator(int index)。最终都会调用listIterator(int index)方法,其中参数表示迭代器开始的位置。在ArrayList源码分析中提到过ListIterator是一个可以指定任意位置开始迭代,并且有两个遍历方法。下面直接看ListItr的实现:
private class ListItr implements ListIterator<E> {
private Node<E> lastReturned;
private Node<E> next;
private int nextIndex;
private int expectedModCount = modCount;//保存当前modCount,确保fail-fast机制
ListItr(int index) {
// assert isPositionIndex(index);
next = (index == size) ? null : node(index);//得到当前索引指向的next节点
nextIndex = index;
}
public boolean hasNext() {
return nextIndex < size;
}
//获取下一个节点
public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
public boolean hasPrevious() {
return nextIndex > 0;
}
//获取前一个节点,将next节点向前移
public E previous() {
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev;
nextIndex--;
return lastReturned.item;
}
public int nextIndex() {
return nextIndex;
}
public int previousIndex() {
return nextIndex - 1;
}
public void remove() {
checkForComodification();
if (lastReturned == null)
throw new IllegalStateException();
Node<E> lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex--;
lastReturned = null;
expectedModCount++;
}
public void set(E e) {
if (lastReturned == null)
throw new IllegalStateException();
checkForComodification();
lastReturned.item = e;
}
public void add(E e) {
checkForComodification();
lastReturned = null;
if (next == null)
linkLast(e);
else
linkBefore(e, next);
nextIndex++;
expectedModCount++;
}
public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (modCount == expectedModCount && nextIndex < size) {
action.accept(next.item);
lastReturned = next;
next = next.next;
nextIndex++;
}
checkForComodification();
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
在ListIterator的构造器中,得到了当前位置的节点,就是变量next。next()方法返回当前节点的值并将next指向其后继节点,previous()方法返回当前节点的前一个节点的值并将next节点指向其前驱节点。由于Node是一个双端节点,所以这儿用了一个节点就可以实现从前向后迭代和从后向前迭代。另外在ListIterator初始时,exceptedModCount保存了当前的modCount,如果在迭代期间,有操作改变了链表的底层结构,那么再操作迭代器的方法时将会抛出ConcurrentModificationException。