导读:我们知道数组集合ArrayList在某个下标插入一个元素add(index,E)的时候,这个下标的所有元素都会向后移动一位,时间复杂为O(n)。在删除某一个下标元素remove(index)的时候,这个下标的所有元素都会向前移动一位,时间复杂也为O(n)。那么,有没有一种数据结构可以降低这个时间复杂度呢?这种数据接口是如何实现的呢?接下来,我们就来学习一下java8中LinkedList,链表集合。
类核心成员
- size 保存链表长度
- first 保存链表头
- last 保存链表尾
- Node 内部类 节点
-
- item 保存节点的值
-
- next 保存下一个节点的地址
-
- prev 保存前一个节点的地址
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable { /** 链表长度*/ transient int size = 0; /** 链表头*/ transient Node<E> first; /** 链表尾*/ transient Node<E> last; /* * 内部类 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类型的链式机构
主要方法的解读
新增的元素使用Node包裹起来,元素使用Node.item保存起来。这个链表的实例last的值是新增元素的地址。当这个链表为空时,first的值是这个新增元素,即是链表头,第一个元素。
新增的元素追加到链表尾部,所以新增的时间复杂度为O(1)。
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
final Node<E> l = last;
//将尾节点设置为这个节点的prev
final Node<E> newNode = new Node<>(l, e, null);
//这个节点设置位尾节点
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
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;
}
}
删除方法,先判断Object o的类型,然后使用了for循环,删除节点。这儿需要注意这个方法是先使用if,然后使用for循环,这样的流程是高效的。如果先使用for循环,然后在判断数据是否null,那么这样的流程会在每一次的for循环里,都会去执行if判断语句,这样会增加代码的执行时间,效率会降低。
删除节点,时间复杂度为O(n)
public boolean remove(Object o) {
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
unlink(x);//删除o 的节点
return true;
}
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
修改方法set(),在node()获取元素方法中,遍历获取元素的时候做了一个细节优化,当index值小于链表长度的一半,则从0开始遍历;否则倒序遍历到index。减少遍历次数。时间复杂度O(n)
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
//当index值小于链表长度的一半,则从0开始遍历
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
//否则倒序遍历到index截至
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
get()方法使用的是node方法获取元素。时间复杂度O(n)
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
add(int index, E element)。第一步判断index是否正常。第二步,如果index等于链表长度则在尾部追加,否则就在链表的中间插入。新增时间复杂度为O(1),插入时间复杂度为O(n)
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
// node(index) 是for循环 时间复杂度O(n)
linkBefore(element, node(index));
}
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++;
}
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++;
}
总结
操作 | 操作 | 新增 | 删除 | 修改 | 查询 | 插入 |
数组集合 | 时间复杂度 | O(1) | O(n) | O(1) | O(1) | O(n) |
链表集合 | 时间复杂度 | O(1) | O(n) | O(n) | O(n) | O(n) |
特点
- 新增操作复杂度为O(1),效率非常高
- 链表有序,可以找到前一个或者后一个元素
- 不需要开辟一块连续的内存区域
- 可以保存相同的数据
- 不需要申明长度,自动增长,效率高
应用场景
- 新增操作频繁,使用链表结合
结合数组集合应用场景总结
未知元素个数,一般使用链表集合
查询频率高,一般使用数组集合。比如for循环查询、代码里多个地方查询元素。