Java集合源码剖析之LinkedList
1 LinkedList概括
1.1 特性
LinkedList是基于双向链表实现的有序集合,它的容量可以动态的增长和缩减,在存储设备许可的范围内容量没有限制。
LinkedList可以在任何位置进行快速地插入和删除操作,但查找元素时必须要遍历整个集合,因此,LinkedList更适合需要频繁的插入或删除的应用场景,而不太适合需要频繁的查找的应用场景。
LinkedList是非线程安全的,只能在单线程环境下使用。
LinkedList实现了Deque接口,因此,具有队列的特性。
LinkedList中的元素可以重复,且可以为null值。
特性总结:有序、不限容量、高效增删、非线程安全、元素可重复。
1.2 数据结构
LinkedList是使用双向链表实现的,每一个元素都单独存储在一个节点中,节点的存储结构如下图所示:
LinkedList的数据结构图如下图所示:
2 LinkedList源码剖析
2.1 继承关系
LinkedList的层次结构如处图所示:
LinkedList的继承关系下所示:
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
......
}
2.2 元素节点
/**
* 元素节点
*/
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;
}
}
2.3 成员属性
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
private static final long serialVersionUID = 8683452581122892189L;
transient int size = 0;
/**
* 头部元素节点
*/
transient Node<E> first;
/**
* 尾部元素节点
*/
transient Node<E> last;
}
2.4 构造方法
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
/**
* 无参构造方法:空的双向链表
*/
public LinkedList() {
}
/**
* 有参构造方法:使用指定的数据元素创建一个双向链表
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
}
2.5 核心方法
2.5.1 添加元素
/**
* 在尾部添加一个元素
*/
public boolean add(E e) {
linkLast(e);
return true;
}
/**
* 在指定位置添加一个元素
*/
public void add(int index, E element) {
// 检查索引合法性
checkPositionIndex(index);
if (index == size) {
linkLast(element);
}
else {
linkBefore(element, node(index));
}
}
/**
* 在尾部批量添加元素
*/
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
/**
* 在指定位置批量添加元素
*/
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;
}
/**
* 在头部添加一个元素
*/
public void addFirst(E e) {
linkFirst(e);
}
/**
* 在尾部添加一个元素
*/
public void addLast(E e) {
linkLast(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) {
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) {
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++;
}
2.5.2 查询元素
/**
* 查询指定位置的元素
*/
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
/**
* 查询第一个元素
*/
public E getFirst() {
final Node<E> f = first;
if (f == null) {
throw new NoSuchElementException();
}
return f.item;
}
/**
* 查询最后一个元素
*/
public E getLast() {
final Node<E> l = last;
if (l == null) {
throw new NoSuchElementException();
}
return l.item;
}
/**
* 获取指定位置的节点
*/
Node<E> node(int 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;
}
}
2.5.3 修改元素
/**
* 修改指定位置的元素
*/
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
2.5.4 删除元素
/**
* 删除一个元素
*/
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;
}
/**
* 删除第一个元素
*/
public E removeFirst() {
final Node<E> f = first;
if (f == null) {
throw new NoSuchElementException();
}
return unlinkFirst(f);
}
/**
* 删除最后一个元素
*/
public E removeLast() {
final Node<E> l = last;
if (l == null) {
throw new NoSuchElementException();
}
return unlinkLast(l);
}
/**
* 清空:删除所有的元素
*/
public void clear() {
for (Node<E> x = first; x != null; ) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
/**
* 从双向链表中删除指定节点
*/
E unlink(Node<E> x) {
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;
}
/**
* 从链表中删除头部结点
*/
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
final E element = f.item;
final Node<E> next = f.next;
f.item = null;
f.next = null; // help GC
first = next;
if (next == null) {
last = null;
}
else {
next.prev = null;
}
size--;
modCount++;
return element;
}
/**
* 从链表中删除尾部节点
*/
private E unlinkLast(Node<E> l) {
final E element = l.item;
final Node<E> prev = l.prev;
l.item = null;
l.prev = null; // help GC
last = prev;
if (prev == null) {
first = null;
}
else {
prev.next = null;
}
size--;
modCount++;
return element;
}
2.5.5 其它方法
public boolean contains(Object o) {
return indexOf(o) != -1;
}
public int indexOf(Object o) {
int index = 0;
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
return index;
}
index++;
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
return index;
}
index++;
}
}
return -1;
}
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
for (Node<E> x = first; x != null; x = x.next) {
result[i++] = x.item;
}
return result;
}
public <T> T[] toArray(T[] a) {
if (a.length < size) {
a = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
}
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next) {
result[i++] = x.item;
}
if (a.length > size) {
a[size] = null;
}
return a;
}
如果觉得本文对您有帮助,请关注博主的微信公众号,会经常分享一些Java和大数据方面的技术案例!