LinkedList源码阅读
前言
我们之前已经进行过ArrayList的源码阅读,我们知道,同样继承了List接口的LinkedList,比起ArrayList而言,LinkedList在新增元素和删除元素时表显要好,那么今天我们就一起看看LinkedList源码中蕴含了什么秘密。
正文
1、LinkedList的成员变量
我们先看下LinkedList的成员变量:
transient int size = 0;
这是LinkedList大小,初始值为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;
指向最后一个节点的指针。
/**
* The number of times this list has been <i>structurally modified</i>.
* Structural modifications are those that change the size of the
* list, or otherwise perturb it in such a fashion that iterations in
* progress may yield incorrect results.
*
* <p>This field is used by the iterator and list iterator implementation
* returned by the {@code iterator} and {@code listIterator} methods.
* If the value of this field changes unexpectedly, the iterator (or list
* iterator) will throw a {@code ConcurrentModificationException} in
* response to the {@code next}, {@code remove}, {@code previous},
* {@code set} or {@code add} operations. This provides
* <i>fail-fast</i> behavior, rather than non-deterministic behavior in
* the face of concurrent modification during iteration.
*
* <p><b>Use of this field by subclasses is optional.</b> If a subclass
* wishes to provide fail-fast iterators (and list iterators), then it
* merely has to increment this field in its {@code add(int, E)} and
* {@code remove(int)} methods (and any other methods that it overrides
* that result in structural modifications to the list). A single call to
* {@code add(int, E)} or {@code remove(int)} must add no more than
* one to this field, or the iterators (and list iterators) will throw
* bogus {@code ConcurrentModificationExceptions}. If an implementation
* does not wish to provide fail-fast iterators, this field may be
* ignored.
*/
protected transient int modCount = 0;
这个属性继承自LinkedList的父类AbstractList。表示这个list进行结构性修改的次数。
2、Node的内部结构
我们再来看下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里面包含了三个成员属性:
(1)E item:当前节点的数据。
(2)Node next:当前节点指向下一个节点的指针。
(3)Node prev:当前节点指向上一个节点的指针。
可以看出Node是一个链表结构,链表上的每一个节点都能沿着链表找到头节点和尾节点。
这样的结构与ArrayList不同,ArrayList是由数组来存储元素,利用数组的特性进行一系列操作。在有索引的情况下,进行get和set操作会比较快速些。而LinkedList由于采用链表结构来存储元素,导致在进行查询操作时,需要利用指针的移动来找到目标元素,相对来说性能比较差。
3、add方法
接下来,我们看看LinkedList的add方法:
/**
* Appends the specified element to the end of this list.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
linkLast(e);
return true;
}
我们看到,在进行新增元素操作时,LinkedList首先调用了linkLast方法:
/**
* 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++;
}
linkLast方法将目标元素作为尾节点放置在链表的末端,并将size和modCount各加1.
4、remove方法
remove方法:
/**
* Removes the first occurrence of the specified element from this list,
* if it is present. If this list does not contain the element, it is
* unchanged. More formally, removes the element with the lowest index
* {@code i} such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
* (if such an element exists). Returns {@code true} if this list
* contained the specified element (or equivalently, if this list
* changed as a result of the call).
*
* @param o element to be removed from this list, if present
* @return {@code true} if this list contained the specified element
*/
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;
}
找到list中指定的第一个出现的元素,如果存在就删除它。
这里我们可以看到,LinkedList在删除元素时,是对链表进行了遍历,通过移动指针来寻找需要的元素。相比较ArrayList而言,ArrayList通过索引来定位目标元素的方式就直接的多。
5、get方法
get方法:
/**
* Returns the element at the specified position in this list.
*
* @param index index of the element to return
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
返回list中指定位置的元素。
这里调用了checkElementIndex方法:
private void checkElementIndex(int index) {
if (!isElementIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* Tells if the argument is the index of an existing element.
*/
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
检查了目标位置未超出list的大小。
调用node方法:
/**
* 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;
}
}
返回了指定元素位置的节点对象。
这里LinkedList将链表一分为二,如果目标元素在前半段,那么LinkedList会从前半段链表从头遍历,通过指针的移动来寻找目标元素;否则LinkedList会从链表的尾节点来从后往前遍历,同样通过移动指针去寻找目标元素。
比较而言,ArrayList在进行get操作时,直接通过数组的索引来定位到目标元素,性能上要好些。