废话很少说,先上测试结果。做者分别在ArrayList和LinkedList的头部、尾部和中间三个位置插入与查找100000个元素所消耗的时间来进行对比测试,下面是测试结果
(感谢@Hosalo的指正,在这里说明一下测试的环境,尾部插入是在空表的基础上测试的,头部和中间位置插入是在已存在100000个元素的表上进行测试的)java
插入 | 查找 | |
---|---|---|
ArrayList尾部 | 26ms | 4ms |
ArrayList头部 | 2887ms | 3ms |
ArrayList中间 | 1936ms | 4ms |
LinkedList尾部 | 28ms | 9ms |
LinkedList头部 | 15ms | 11ms |
LinkedList中间 | 12310ms | 11387ms |
咱们把Java中的ArrayList和LinkedList就是分别对顺序表和双向链表的一种实现,因此在进行源码分析以前,咱们先来简单回顾一下数据结构中的顺序表与双向链表中的关键概念node
因此咱们潜意识会认为:ArrayList查找快,增删慢。LinkedList查找慢,增删快。但实际上真的是这样的吗?咱们一块儿来看看吧。数组
测试程序代码基本没有什么养分,这里就不贴出来了,可是得把程序的运行结果贴出来,方便逐个分析。数据结构
ArrayList尾部插入100000个元素耗时:26ms
LinkedList尾部插入100000个元素耗时:28ms
ArrayList头部插入100000个元素耗时:859ms
LinkedList头部插入100000个元素耗时:15ms
ArrayList中间插入100000个元素耗时:1848ms
LinkedList中间插入100000个元素耗时:15981ms
ArrayList头部读取100000个元素耗时:7ms
LinkedList头部读取100000个元素耗时:11ms
ArrayList尾部读取100000个元素耗时:12ms
LinkedList尾部读取100000个元素耗时:9ms
ArrayList中间读取100000个元素耗时:13ms
LinkedList中间读取100000个元素耗时:11387ms
函数
add(E e)方法源码分析
public boolean add(E e) {
// 检查是否须要扩容
ensureCapacityInternal(size + 1); // Increments modCount!!
// 直接在尾部添加元素
elementData[size++] = e;
return true;
}
复制代码
能够看出,对ArrayList的尾部插入,直接插入便可,无须额外的操做。性能
LinkedList中定义了头尾节点测试
/** * Pointer to first node. */
transient Node<E> first;
/** * Pointer to last node. */
transient Node<E> last;
复制代码
add(E e)方法,该方法中调用了linkLast(E e)方法spa
public boolean add(E e) {
linkLast(e);
return true;
}
复制代码
linkLast(E e)方法,能够看出,在尾部插入的时候,并不须要从头开始遍历整个链表,由于已经事先保存了尾结点,因此能够直接在尾结点后面插入元素指针
/** * Links e as last element. */
void linkLast(E e) {
// 先把原来的尾结点保存下来
final Node<E> l = last;
// 建立一个新的结点,其头结点指向last
final Node<E> newNode = new Node<>(l, e, null);
// 尾结点置为newNode
last = newNode;
if (l == null)
first = newNode;
else
// 修改原先的尾结点的尾结点,使其指向新的尾结点
l.next = newNode;
size++;
modCount++;
}
复制代码
对于尾部插入而言,ArrayList与LinkedList的性能几乎是一致的
add(int index, E element)方法,能够看到经过调用系统的数组复制方法来实现了元素的移动。因此,插入的位置越靠前,须要移动的元素就会越多
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1); // Increments modCount!!
// 把原来数组中的index位置开始的元素所有复制到index+1开始的位置(其实就是index后面的元素向后移动一位)
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
// 插入元素
elementData[index] = element;
size++;
}
复制代码
add(int index, E element)方法,该方法先判断是不是在尾部插入,若是是调用linkLast()方法,不然调用linkBefore(),那么是否真的就是须要重头开始遍历呢?咱们一块儿来看看
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
复制代码
在头尾之外的位置插入元素固然得找出这个位置在哪里,这里面的node()方法就是关键所在,这个函数的做用就是根据索引查找元素,可是它会先判断index的位置,若是index比size的一半(size >> 1,右移运算,至关于除以2)要小,就从头开始遍历。不然,从尾部开始遍历。从而能够知道,对于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;
}
}
复制代码
这个函数的工做就只是负责把元素插入到相应的位置而已,关键的工做在node()方法中已经完成了
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++;
}
复制代码
第一次在掘金撸博客,但愿之后多给你们分享一些有趣的知识!