序:
ArrayList基于数组来实现集合的功能,其内部维护了一个可变长的对象数组,集合内所有对象存储于这个数组中,并实现该数组长度的动态伸缩。LinkedList基于链表来实现集合的功能,其实现了静态类Node,集合中的每个对象都由一个Node保存,每个Node都拥有到自己的前一个和后一个Node的引用
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
transient int size = 0;
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
可以看到linkedlist实现了list,deque接口。有头结点,尾节点,容量size。
从节点属性看。是个双向的节点, item是数据,next,prev是指向。
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;
}
}
二 方法:
/**
* Links e as first element. 插入头结点
*/
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++;
}
/**
* Links e as last element.插入尾节点
*/
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++;
}
/**
* Inserts element e before non-null Node succ.指定非空节点succ前面插入节点
*/
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
final Node<E> pred = succ.prev;//获取succ的前一个节点
final Node<E> newNode = new Node<>(pred, e, succ);
succ.prev = newNode;//让succ节点头部指向新建的节点
if (pred == null) //之前的节点为空,则认为新插入的为头结点
first = newNode;
else
pred.next = newNode; //否则之前节点的下一个节点指向新插入的节点
size++;
modCount++;
}
unlinkFirst、unlinkLast是个添加对应相反的过程
/**
* Unlinks non-null node x.//删除非空节点x
*/
E unlink(Node<E> x) {
// assert x != null;
final E element = x.item;
final Node<E> next = x.next; //x节点的下一个节点
final Node<E> prev = x.prev; //x节点的前一个节点
if (prev == null) { //如果前节点为空,则头结点指向x的下一个节点(x被删除了)
first = next;
} else {
prev.next = next; //否则前节点的下一个节点指向x节点的下节点(跨过x节点)
x.prev = null;
}
if (next == null) {//如果后节点为空,说明x的前节点为尾节点
last = prev;
} else {
next.prev = prev;//否则后节点的前向节点指向x节点的前节点(扩过x节点)
x.next = null;
}
x.item = null;
size--;
modCount++;
return element;
}
/**
* Returns the (non-null) Node at the specified element index.
*/
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;
}
}
node的方法,改变了我之前以为从头到尾一直遍历的查找的印象。还是有优化的用了二分判断,要么从头查,要么从尾倒查。
上面私有的方法,可见主要是指针的操作,插入、删除是比较快的。
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));
}
addAll要复杂些。
//删除指定位置节点
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 boolean removeLastOccurrence(Object o) {
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
//修改方法
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
//查找方法。从头开始
public int indexOf(Object o) {
int index = 0;
if (o == null) {//针对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;
}
另外,linkedlist还实现了deque接口。所以队列的接口方法也支持,如:
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);
}
三 总结
- 双向链表实现
- 元素时有序的,输出顺序与输入顺序一致
- 允许元素为 null
- 所有指定位置的操作都是从头开始遍历进行的
- 和 ArrayList 一样,不是同步容器
- 添加节点没有扩容: public boolean add(E e) {
linkLast(e);
return true;
}
arraylist对比linkedlist:
ArrayList的随机访问更高,基于数组实现的ArrayList可直接定位到目标对象,而LinkedList需要从头Node或尾Node开始向后/向前遍历若干次才能定位到目标对象
LinkedList在头/尾节点执行插入/删除操作的效率比ArrayList要高
由于ArrayList每次扩容的容量是当前的1.5倍,所以LinkedList所占的内存空间要更小一些
二者的遍历效率接近,但需要注意,遍历LinkedList时应用iterator方式,不要用get(int)方式,否则效率会很低
参考:
https://blog.youkuaiyun.com/u011240877/article/details/52876543