一. LinkedList概述
LinkedList是双向链表实现的ListLinkedList是非线程安全的LinkedList元素允许为null,允许重复元素LinkedList是基于链表实现的,因此插入删除效率高,查找效率低。LinkedList是基于链表实现的,因此不存在容量不足的问题。
二. LinkedList源码分析
LinkedList是由双链表的数据结构组成的。
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
//元素个数
transient int size = 0;
//指向第一个节点的指针不变:
如果first==null,则last==null。
transient Node<E> first;
//指向最后一个指针
transient Node<E> last;
//构造方法
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
private static class Node<E> {
E item;
//下一个node的引用
Node<E> next;
//上一个node的引用
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
}
三. LinkedList常用方法及源码分析
3.1 addFirst()
// 从头插入
public void addFirst(E e) {
linkFirst(e);
}
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
if (f == null)
// 当前List中没有元素,
size=0
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
当链表中没有元素时,会将新加入的元素newNode指向last和first,否则指向f.prev。
3.2 add()和addLast
//从尾部插入
public boolean add(E e) {
linkLast(e);return true;
}
public void addLast(E e) {
linkLast(e);
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
// 当前List中没有元素,
size=0;
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
}
当链表中没有元素时,newNode指向first节点,否则指向l.next节点
3.2 remove()和removeFirst()
// 移除首节点,并返回该节点的元素值
public E remove() {
return removeFirst();
}
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
// 删除首节点f
private E unlinkFirst(Node<E> f) {
final E element = f.item;
final Node<E> next = f.next;
f.item = null;
f.next = null;
first = next;
if (next == null)
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}
如果当f节点为空时,抛出NoSuchElementException异常。
3.3removeLast()
//从尾部移除
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
private E unlinkLast(Node<E> l) {
final E element = l.item;
final Node<E> prev = l.prev;
l.item = null;
l.prev = null;
last = prev;
if (prev == null)
first = null;
else
prev.next = null;
size--;
modCount++;
return element;
}
如果当l节点为空时,抛出NoSuchElementException异常。
3.4 remove(int index)
//根据索引删除
public E remove(int index) {
checkElementIndex(index);// 检查索引index范围
return unlink(node(index));
}
private void checkElementIndex(int index) {
if (!isElementIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
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;
}
根据索引删除时会先执行checkElementIndex(index)方法检查索引是否在范围内,如不在,抛出IndexOutOfBoundsException异常。
四. 总结
1、链表 (Linked List) 是一种常见的基础数据结构,是一种线性表,但是链表不会按线性表的顺序存储数据,而是每个节点里存到下一个节点的地址
2、它是一个集合,可以根据索引随机访问集合中的元素,还实现了Deque接口
3、LinedList是一个List集合,它的实现方式和ArrayList是完全不同的,ArrayList的底层是通过一个动态的Object[]数组实现的,而LinkedList的底层是通过链表来实现的,因此它的随机访问速度是比较差的,但是它的删除,插入操作很快。
4、LinkedList是非线程安全的,只在单线程下适合使用。