前言
LinkedList实现了List与Deque接口。数据结构为双端链表结构,当执行随机位置插入和删除的操作时不需要跟ArrayList一样执行一次复制移动数据的过程,只需要修改前后节点的前后连接即可,所以可以相对高效的执行的插入和移除操作。
源码分析
构造函数(空构造方法)
/**
* Constructs an empty list.
*/
public LinkedList() {
}
构造函数(集合作为参数的构造方法)
// 先执行空构造方法,然后将集合中的数据遍历按add方法添加到列表中
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
内部类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;
}
}
add(E e)源码分析
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
// 此处的last为LinkedList的实例变量,用于执行该链表的最后一个节点
final Node<E> l = last;
// 创建一个新的节点
final Node<E> newNode = new Node<>(l, e, null);
// 默认添加节点是将节点添加到尾端,所以需要将链表的实例变量last执行新添加的节点
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);
// 如果索引跟链表尺寸一样,则添加到链表尾端,否则根据索引位置执行linkBefore方法
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
void linkBefore(E e, Node<E> succ) {
// 双端链表插入原理就是打破原来的前驱与后驱指向,将新加入的成员插入到中间,假设a.next = b, b.pre = a,由于新节点c的插入,所以会变成a.next = c, c.pre = a, c.next = b, b.pre = c
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(Collection c)源码分析
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;
// index == size则代表从尾端插入
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);
// 如果前驱为null,则指向自身,否则前驱节点的后驱指向当前新建的节点,相当于构建了新插入节点与前驱节点的指向关系
if (pred == null)
first = newNode;
else
pred.next = newNode;
// 即将进入下一个循环,所以需要将pred对象指向到新插入的节点,作为下一个插入节点的前驱指向
pred = newNode;
}
// 插入完集合中所有的对象后,需要把之前被打破的后驱链路连接起来,也就是之前的succ对象,代表index索引对应的对象
if (succ == null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
get(int index)源码分析
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
/**
* 根据索引找到对应节点,假设链表比较长,查找节点时需要遍历链表,通过节点的后驱找到下一个节点,再通过下一个节点的后驱找到下下个节点,所以如果在数据量比较大的情况下,链表不适合做查找操作
**/
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;
}
}
contains(Object o)源码分析
public boolean contains(Object o) {
return indexOf(o) != -1;
}
/**
* 遍历列表,判断节点中的值与查找对象是否相同,返回索引位置
/**
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;
}
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;
}