一、LinkedList简介
链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的地址。LinkedList的增删改操作可以按双向链表,双端队列的方式操作。
LinkedList实现了Serialiable接口,和ArrayList一样虽然实现了序列化接口但是存储数据的变量依旧被transient修饰。LinkedList重写了writeObject()和readObject()来进行序列化和反序列化。
LinkedList实现了Deque接口所以链表的增加删除可以和队列一样进行入列出列,不过底层调用的方法基本一致,所以下面介绍方法我就略过了。
LinkedList和ArrayList的区别?
- LinkedList底层是通过链表来存储数据的,ArrayList底层是通过数组来存储数据的。
- 因为LinkedList是链表存储数据所以LinkedList查询数据的效率较低但增加和删除快。而ArrayList是数组存储数据所以查询的效率高但是增加和删除的慢。
二、源码分析
1)变量
/**
* 记录链表的容量
*/
transient int size = 0;
/**
* 指向链表的头节点
*/
transient Node<E> first;
/**
* 指向链表的尾节点
*/
transient Node<E> last;
2)构造方法
/**
* 无参构造方法
*/
public LinkedList() {
}
/**
* 有参构造方法,将某个集合转为LinkedList
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
3)add/push方法,add方法插入链表的方法是:尾插法,因为记录了尾节点所以不会有效率低的问题
/**
* 往链表中添加元素,尾插法
*/
public boolean add(E e) {
linkLast(e);
return true;
}
/**
* 头插法,在链表头部插入元素
*/
public void addFirst(E e) {
linkFirst(e);
}
/**
* 在指定位置加入元素
*/
public void add(int index, E element) {
//判断下标是否越界
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
/**
* 在链表尾部插入元素
*/
public void addLast(E e) {
linkLast(e);
}
/**
* 在指定位置的节点前插入一个集合
*
*/
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;
if (index == size) {
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;
}
/**
* 找到指定位置的节点并返回
*/
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中实现了多个私有方法供共有方法调用。
/**
* 在链表头部添加元素
*/
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++;
}
/**
* 在链表尾部添加元素
*/
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++;
}
4)remove/poll方法
/**
* 删除第一个元素
*/
public E remove() {
return removeFirst();
}
/**
* 删除指定位置的元素
*/
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
/**
* 删除指定元素
*/
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 (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;
}
5)writeObject()和readObject(),因为存储数据的链表都被transient修饰所以需要自己重写这两个方法将数据序列化。
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();
// Write out size
s.writeInt(size);
// Write out all elements in the proper order.
for (Node<E> x = first; x != null; x = x.next)
s.writeObject(x.item);
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read in size
int size = s.readInt();
// Read in all elements in the proper order.
for (int i = 0; i < size; i++)
linkLast((E)s.readObject());
}