LinkedList也像ArrayList一样实现了基本的List接口,它相对于ArrayList来讲在插入和删除元素操作方面要更高效,但是在随机查询元素操作方面要逊色些。
接下来分析下LinkedList的内在结构。
LinkedList底层是基于双向链表的实现,核心是用Node节点存储元素:
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;
}
}
Node节点除了存储元素值和下一节点外,还存储了上一节点,这也意味着其可以逆向遍历。
LinkedList的成员变量:
/**
* 集合内元素数量
*/
transient int size = 0;
/**
* 头节点
*/
transient Node<E> first;
/**
* 尾节点
*/
transient Node<E> last;
构造方法仅一个空构造和将整个集合复制的构造方法:
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
添加元素
public boolean add(E e) {
linkLast(e);
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++;
}
就是非常简单的插入链表操作,注意更新头节点和尾节点信息。
也可以选择在指定索引处添加元素:
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++;
}
这里涉及到需要根据索引值来查找集合元素的位置,链表不像数组那样可以一下定位到位置,LinkedList是这么处理的:
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可双向遍历,那么只需判断index是在集合的左半区还是右半区,再选择用头结点还是尾节点进行遍历即可。这里用到了右移运算符,size>>1相当于size除以2,移位运算符操作比正常乘除操作要快。
LinkedList的增加元素操作还有以下几个方法可用:
public boolean offer(E e) {
return add(e);
}
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
public boolean offerLast(E e) {
addLast(e);
return true;
}
public void push(E e) {
addFirst(e);
}
为什么要有那么多相同作用的方法,根据《Java编程思想》里的解释是说可以使得这些方法名在特定用法的上下文环境中更加适用(比如在Queue中),你浏览Queue的接口会发现,它在LinkedList的基础上添加了element()、offer()、peek()、poll()、remove()方法,以使其可以成为一个Queue的实现。
删除元素
删除操作包括删除头节点元素、尾节点元素、根据元素删除、根据索引删除。
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 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 remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
具体实现删除方法如下:
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) {
// assert l == last && l != null;
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;
}
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;
}
就是改变节点的引用,注意要删除的节点要释放内存。
修改元素
根据索引修改:
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
获取元素
获取头结点、尾节点、根据索引查询元素。
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;
}
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
根据元素值查找其在集合中的位置:
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 int lastIndexOf(Object o) {
int index = size;
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (x.item == null)
return index;
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (o.equals(x.item))
return index;
}
}
return -1;
}
这里要提下lastIndexOf方法不是查倒数在哪个位置,而是沿着链表倒着查询,返回的仍然是元素在集合中的正向顺序位置。
LinkedList还有很多获取元素的方法:
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
public E element() {
return getFirst();
}
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
public E pop() {
return removeFirst();
}
这些方法的作用前面有提到,是为了适配特定用法,比如Stack中。
总结下,LinkedList在插入链表头部和链表尾部时操作较快(相比于ArrayList要判断和扩容),但是在插入指定位置操作上需要遍历链表;查找元素也是,需要遍历链表找到该元素,相对较慢。
最后放下LinkedList和ArrayList的区别:
- ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。
- 对于随机访问get和set,ArrayList要优于LinkedList,因为LinkedList要沿着节点遍历。
- 对于新增和删除操作,LinedList比较快,因为ArrayList要移动数据和复制数组。