前言
LinkedList底层采用的双向链表结构,实现了List和Deque接口。 和 ArrayList 一样,LinkedList 也支持空值和重复值。由于 LinkedList 基于链表实现,存储元素过程中,无需像 ArrayList 那样进行扩容。通过引用来表示上一个节点和下一个节点的关系。LinkedList 是非线程安全的集合类,如果多个线程同时访问链接列表,并且至少有一个线程在结构上修改列表,则必须在外部进行同步。 (结构修改是添加或删除一个或多个元素的任何操作;仅设置元素的值不是结构修改。) 这通常通过在自然封装列表的对象上进行同步来实现。 如果没有这样的对象存在,列表应该使用Collections.synchronizedList方法“包装”。 这最好在创建时完成,以防止意外的不同步访问列表:
List list = Collections.synchronizedList(new LinkedList(...));
一、源码分析
1.继承体系
LinkedList构造方法及变量(示例):
/**
* 集合中的元素数量
*/
transient int size = 0;
/**
* 指向第一节点的指针
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;
/**
* 最后一个节点指针
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
/**
* 构造一个空列表.
*/
public LinkedList() {
}
/**
* 构造一个列表,其中包含指定集合的元素,按照集合返回元素的顺序排列
* @param c
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
2.新增
代码如下(示例):
/**
* 将元素插入链表头部
*/
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
// 如果first等于null 说明当前链表第一次添加元素,既是头节点也是尾节点
if (f == null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
/**
* 将元素插入链表尾部
*/
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
// 如果last等于null 说明当前链表第一次添加元素,既是头节点也是尾节点
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
/**
* 将元素e 插入到 succ 之前的位置
*/
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
final Node<E> pred = succ.prev;
//把需要插入的元素包装成Node节点,指明上一个节点和下一个节点
final Node<E> newNode = new Node<>(pred, e, succ);
//把 succ 节点的上一个节点指向新的节点
succ.prev = newNode;
//如果pred==null,当前链表还未添加节点
if (pred == null)
first = newNode;
else
//把 succ 上一个节点的下个节点指向新的节点
pred.next = newNode;
size++;
modCount++;
}
/**
* 将元素插入链表尾部
* @param e
* @return
*/
public boolean add(E e) {
linkLast(e);
return true;
}
/**
* 将元素e 插入到 index 之前的位置
* @param index
* @param element
*/
public void add(int index, E element) {
//边界值检查
checkPositionIndex(index);
//如果index是链表尾部位置,直接将元素节点插入链表尾部即可
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
/**
* 返回指定元素索引处的(非空)节点
* @param index
* @return
*/
Node<E> node(int index) {
// assert isElementIndex(index);
// (>>) 位运算,右移一位,其实就是除以2
//链表的大小除以2 小于 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;
}
}
/**
* 将元素插入链表头部
* @param e
*/
public void addFirst(E e) {
linkFirst(e);
}
/**
* 将元素插入链表尾部
* @param e
*/
public void addLast(E e) {
linkLast(e);
}
/**
* 在链表末尾追加指定集合中的所有元素
* @param c
* @return
*/
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
/**
* 从指定位置开始,将指定集合中的所有元素插入到此链表中
* @param index
* @param c
* @return
*/
public boolean addAll(int index, Collection<? extends E> c) {
//index 边界值检查
checkPositionIndex(index);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;
//判断index是链表尾部位置
if (index == size) {
succ = null;
pred = last;
} else {
// index不是链表尾,则获取索引位置的节点
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;
}
// succ == null 说明index是链表尾部位置 for完成后newNode就是最后一个节点
if (succ == null) {
last = pred;
} else {
//for完成后newNode的下个节点指向 从索引位置获取的节点
pred.next = succ;
//从索引位置获取节点的上一个节点指向 newNode节点
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
上面是插入过程的源码,对源码进行了逐行分析,应该不难看懂。上面两个 add 方法只是对操作链表的方法做了一层包装,核心逻辑在 linkBefore 和 linkLast 中。这里以 linkBefore 为例,它的逻辑流程如下:
创建新节点,并指明新节点的前驱和后继
将 succ 的前驱引用指向新节点
如果 succ 的前驱不为空,则将 succ 前驱的后继引用指向新节点
对应于下图:
2.删除
代码如下(示例):
/**
* 删除第一个出现的元素o
* @param o
* @return
*/
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找到需要删除的节点
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
/**
* 移除列表中指定位置的元素
* @param index
* @return
*/
public E remove(int index) {
//边界值检查
checkElementIndex(index);
// 通过 node 方法找到节点,并调用 unlink 将节点从链表中移除
return unlink(node(index));
}
/**
* 移除非空节点x的链接
*/
E unlink(Node<E> x) {
// assert x != null;
final E element = x.item;
//x的下一个节点
final Node<E> next = x.next;
//x的上一个节点
final Node<E> prev = x.prev;
//prev==null 说明删除的是头节点,把x节点下一个节点指向头节点
if (prev == null) {
first = next;
} else {
// 将 x 的上个的节点指向 x 的下一个节点
prev.next = next;
// 将 x 指向的上一个节点置空,断开与上一个的链接
x.prev = null;
}
//next==null 说明删除的是尾节点,把x节点上一个节点指向尾节点
if (next == null) {
last = prev;
} else {
// 将 x 的后继的前驱指向 x 的前驱
next.prev = prev;
// 将 x 的后继引用置空,断开与后继的链接
x.next = null;
}
// 将 item 置空,方便 GC 回收
x.item = null;
size--;
modCount++;
return element;
}
3.查找
代码如下(示例):
/**
* 返回此列表中指定位置的元素。
* @param index
* @return
*/
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
/**
* 返回指定元素索引处的(非空)节点
* @param index
* @return
*/
Node<E> node(int index) {
// assert isElementIndex(index);
// (>>) 位运算,右移一位,其实就是除以2
//链表的大小除以2 小于 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;
}
}
LinkedList 底层基于链表结构,无法向 ArrayList 那样随机访问指定位置的元素。LinkedList 需要从链表头结点(或尾节点)向后(向前)查找,平均:(N+1)/2次
4.LinkedList(CRUD性能分析)
对LinekdList操作的性能分析:
-
1.增加操作:
- 双向链表可以直接获取自己的第一一个和最后一个节点
- 如果新增的元素在第一一个或最后一个位置,那么操作只有1次
-
2.删除操作(fremovefisrt,removelast))
- 如果删除第一个元素:操作一次
- 如果操作最后一个元素:操作一次
- 如果删除中间的元素,找到元素节点平均操作:(1+N)/2次。找到节点之后做删除操作: 1次.
-
3.查询操作:
- 平均:(N+1)/2次
-
4.修改操作:
- 平均:(N+1)/2次
-
基于数组的列表和基于链表的列表性能对比:
- ArrayList: 查询更新较快,新增和删除较慢.
- LinkedList: 查询更新较慢,新增和删除较快.
完
才疏学浅,难免会有纰漏,如果你发现了错误的地方,可以在留言区提出来,我对其加以修改。
感谢您的阅读