Before it comes to our List collection have said the LinkedList. But more than just a LinkedList List collection implementation, which is a two-way queue implementation.
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
LinkedList implements List interface not only, but also to achieve the Deque interface. So this section we will talk about the two-way queue feature LinkedList.
principle
To understand the principles of LinkedList, we will class member variables, constructors, two core methods introduced one by one.
Class member variables
// 链表大小
transient int size = 0;
// 首节点
transient Node<E> first;
// 尾节点
transient Node<E> last;
// 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;
}
}
LinkedList embodiment can be seen using the implementation list node, and each node has a predecessor and successor nodes.
Construction method
There are two LinkedList constructor:
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
Construction method is relatively simple, there is no in-depth introduction.
The core method
Several methods LinkedList with double-ended queue associated to: offerFirst, offerLast, pollFirst, pollLast.
offerFirst
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
public void addFirst(E e) {
linkFirst(e);
}
// 将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++;
}
offerLast
public boolean offerLast(E e) {
addLast(e);
return true;
}
public void addLast(E e) {
linkLast(e);
}
// 将e节点作为末尾节点插入
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++;
}
pollFirst
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
// 删除头结点
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;
}
pollLast
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
// 删除尾节点
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;
}
It can be seen either inserted or deleted, poll and offer operations are relatively simple, with emphasis on modification and maintenance of reference.
to sum up
LinkedList is not just a simple implementation of List, which is also a two-way queue implementation.