前言
jdk 版本 jdk1.8.0_161
结构图
说明:ArrayList 中提到的这里省略
AbstractSequentialList:List 接口 的骨架实现,对于 需要 顺序 存取(sequential access) 的 数据类型 需要 继承 该抽象类,如 LinkedList 的链接列表。关于链接列表查看 维基百科 和 相关翻译博文。
Queue:队列的实现,先进先出 (FIFO:first in first out);不允许插入 null 值。
Deque:支持在两端插入和删除的 线性集合(双端队列)。double ended queue 的 缩写,读作 ' deck'。该接口定义了存取线性集合两端数据的 方法。既可以用作 先进先出(FIFO)的 队列,也可以用作 后进先出(LIFO:last in first out)的 栈。
LinkedList:List接口 和 Deque 接口 的双向链表的实现。实现了list 所有的可选操作,允许 null 值。
操作 list 中的索引 需要 从头到尾遍历 整个list.
源码分析:
LinkedList 是一个双向链表的实现,链表 是 一个 由 节点组成的 线性集合。双向链表 的节点 有三个字段信息:值信息,前一个节点的指针信息,后一个节点的指针信息。所以 LinkedList 的 内部类 Node 的源码如下,代表链接列表中的一个节点:
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;
}
}
LinkedList 保存了第一个节点 和 最后一个节点:要想获取其他索引处的节点信息,必须经过循环来获取
/**
* 第一个节点的指针
*/
transient Node<E> first;
/**
* 最后一个节点的指针
*/
transient Node<E> last;
数据结构确定之后,剩下的就是 增删改查操作的实现。
新增操作
向list尾部插入指定元素:重写了 AbstractList 中的方法:时间复杂度为 O(1)
public boolean add(E e) {
linkLast(e);
return true;
}
/**
* 将 e 链接为 最后一个元素
* ① 将 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++;
}
向指定位置插入指定元素:需要循环数据,时间复杂度 O(n)
/**
* 在 list 的指定位置插入指定元素
* 当前位置的元素 和 后继元素向右移动
*/
public void add(int index, E element) {
checkPositionIndex(index); //检验 索引的合法性
if (index == size) //在尾部插入
linkLast(element);
else
linkBefore(element, node(index)); //在节点之前插入元素
}
/**
* 返回 指定索引位置的 非空 节点
*/
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) { // index 偏向于首端,就从头循环,循环到 index 位置
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next; // index -1 处的 next 节点 即为 index 的节点
return x;
} else { // index 偏向于尾端就从尾部开始循环到 index 位置
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev; // index +1 处节点的 prev 节点即为 index 处的 节点
return x;
}
}
/**
* 在非空节点 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++;
}
删除操作
删除:
删除指定元素:因为 LinkedList 是允许 重复元素出现的,所以删除只会删除元素第一次出现的节点,需要循环。时间复杂度 O(n)
public boolean remove(Object o) {
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) { //遍历 所有 node,直到出现空节点
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;
}
/**
* 删除 非空节点 x
* ① 修改前一个节点的 字段信息
* ② x 节点的 前一个节点指针置为 null
* ③ 修改后一个节点的字段信息
* ④ x 节点的 后一个节点 和 值 置为 null
*/
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;
}
删除指定位置的元素:需要先查询索引对应的 节点,这个操作需要遍历 集合的一半数据。时间复杂度 O(n/2)
public E remove(int index) {
checkElementIndex(index); //检查 index 是否合法
return unlink(node(index));
}
/**
* 返回 指定索引位置的 非空 节点
*/
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) { // index 偏向于首端,就从头循环,循环到 index 位置
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next; // index -1 处的 next 节点 即为 index 的节点
return x;
} else { // index 偏向于尾端就从尾部开始循环到 index 位置
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev; // index +1 处节点的 prev 节点即为 index 处的 节点
return x;
}
}
修改操作
修改指定位置的元素:node(index) 方法 需要循环一半数据。时间复杂度 O(n/2)
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
查询操作
查询指定位置的元素:node(index) 方法 需要循环一半数据,相对耗时。时间复杂度 O(n/2)
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
总结: LinkedList 在 涉及到索引的操作时,为了获取 该 索引对应的 节点对象(Node),都需要进行循环操作。新增元素 和删除元素(不是在头部和尾部) 复杂度并没有真正意义上的 为 O(1)