1、概述
LinkedList与ArrayList一样实现List接口,只是ArrayList是List接口的大小可变数组的实现,LinkedList是List接口链表的实现。基于链表实现的方式使得LinkedList在插入和删除时更优于ArrayList,而随机访问则比ArrayList逊色些。
LinkedList实现所有可选的列表操作,并允许所有的元素包括null。
除了实现 List 接口外,LinkedList 类还为在列表的开头及结尾 get、remove 和 insert 元素提供了统一的命名方法。这些操作允许将链接列表用作堆栈、队列或双端队列。
此类实现 Deque 接口,为 add、poll 提供先进先出队列操作,以及其他堆栈和双端队列操作。
所有操作都是按照双重链接列表的需要执行的。在列表中编索引的操作将从开头或结尾遍历列表(从靠近指定索引的一端)。
同时,与ArrayList一样此实现不是同步的。
2、源码的分析
2.1、LinkList 的定义 。
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
从这段代码中我们可以清晰地看出LinkedList继承AbstractSequentialList,实现List、Deque、Cloneable、Serializable。其中AbstractSequentialList提供了 List 接口的骨干实现,从而最大限度地减少了实现受“连续访问”数据存储(如链接列表)支持的此接口所需的工作,从而以减少实现List接口的复杂度。Deque一个线性 collection,支持在两端插入和移除元素,定义了双端队列的操作。
2.2 、属性
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;
其中size表示的LinkedList的大小,header表示链表的表头,Entry为节点对象。
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;
}
}
上面为Entry对象的源代码,Entry为LinkedList的内部类,它定义了存储的元素。该元素的前一个元素、后一个元素,这是典型的双向链表定义方式。
2.3、构造函数
/**
* Constructs an empty list.
*/
public LinkedList() {
}
/**
* Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ public LinkedList(Collection<? extends E> c) { this(); addAll(c); }
LinkedList()构造一个空列表。里面没有任何元素.
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index); //若插入的位置小于0或者大于链表长度,则抛出IndexOutOfBoundsException异常
Object[] a = c.toArray();
int numNew = a.length;//插入元素的个数
if (numNew == 0)
return false;
Node<E> pred, succ; //表示的是插入节点的前一个节点 和 插入当前的节点
if (index == size) { //表示的是当两个都为 0
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; //指向下个节点
}
if (succ == null) { //表示的是插入的节点是最后一个
last = pred;
} else { //表示插入的节点不是最后一个
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
在addAll()方法中,涉及到了两个方法,一个是entry(int index),该方法为LinkedList的私有方法,主要是用来查找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;
}
}
如果对数据结构有点了解,对上面所涉及的内容应该问题,我们只需要清楚一点:LinkedList是双向链表,其余都迎刃而解。
由于篇幅有限,下面将就LinkedList中几个常用的方法进行源码分析。
2.4、添加方法
1、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) //如果说 当前为 null
first = newNode;
else
l.next = newNode; //不为空
size++;
modCount++;
}
linkLast 方法 做的就是 构建一个新节点newEntry,然后修改其前后的引用。
LinkedList还提供了其他的增加方法:
add(int index, E element):在此列表中指定的位置插入指定的元素。
addAll(Collection<? extends E> c):添加指定 collection 中的所有元素到此列表的结尾,顺序是指定 collection 的迭代器返回这些元素的顺序。
addAll(int index, Collection<? extends E> c):将指定 collection 中的所有元素从指定位置开始插入此列表。
AddFirst(E e): 将指定元素插入此列表的开头。
addLast(E e): 将指定元素添加到此列表的结尾。
2、 add(int index, E element):在此列表中指定的位置插入指定的元素。
public void add(int index, E element) {
checkPositionIndex(index); //判断下标是不是错误的
if (index == size) //添加到最后
linkLast(element);
else //添加到之前
linkBefore(element, node(index));
}
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++;
}
/**
* Links e as first element.
*/
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++;
}
2.5、移除的方法
remove(Object o):从此列表中移除首次出现的指定元素(如果存在)。该方法的源代码如下public boolean remove(Object o) {
if (o == null) { //判断移除的元素是不是 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;
}
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;
}
其他的移除方法:
clear(): 从此列表中移除所有元素。
remove():获取并移除此列表的头(第一个元素)。
remove(int index):移除此列表中指定位置处的元素。
remove(Objec o):从此列表中移除首次出现的指定元素(如果存在)。
removeFirst():移除并返回此列表的第一个元素。
removeFirstOccurrence(Object o):从此列表中移除第一次出现的指定元素(从头部到尾部遍历列表时)。
removeLast():移除并返回此列表的最后一个元素。
removeLastOccurrence(Object o):从此列表中移除最后一次出现的指定元素(从头部到尾部遍历列表时)。
2.6、获取元素
2.5、查找方法
对于查找方法的源码就没有什么好介绍了,无非就是迭代,比对,然后就是返回当前值。
get(int index):返回此列表中指定位置处的元素。
getFirst():返回此列表的第一个元素。
getLast():返回此列表的最后一个元素。
indexOf(Object o):返回此列表中首次出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1。
lastIndexOf(Object o):返回此列表中最后出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1。
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}