ArrayList与LinkedList获取指定元素对比(源码分析)

简介: ArrayList与LinkedList获取指定元素对比(源码分析)

ArrayList与LinkedList获取指定元素对比(源码分析)


ArrayList获取指定元素

  1. 首先在主函数中使用ArrayList生成一个size为一千万的List实例list
int max = 10000000;
List<String> list = new ArrayList<>(max);
for (int i = 0; i < max; i++) {
    list.add("a");
}
/**
 * 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) {
    rangeCheck(index);
    return elementData(index);
}


可以看到,首先使用rangeCheck对index进行检验;然后点进去elementData:

@SuppressWarnings("unchecked")
E elementData(int index) {
    return (E) elementData[index];
}
  1. 发现直接返回了elementData数组中下标为index的元素,时间复杂度为O(1)
  2. 测试一下使用get方法来获取最后一个元素所需的时间:
long startTime = System.currentTimeMillis();
list.get(max - 1).hashCode();
long endTime = System.currentTimeMillis();
System.out.println("ArrayList获取最后一个元素所需时间: "+(endTime - startTime));

运行结果如下:

ArrayList获取最后一个元素所需时间: 0
  1. 发现该方法运行时间几乎为0,因为无需遍历元素,直接根据index返回数据即可;

LinkedList获取指定元素

  1. 首先在主函数中使用LinkedList生成一个size为一千万的List实例list
int max = 10000000;
List<String> list = new LinkedList<>();
for (int i = 0; i < max; i++) {
    list.add("a");
}


来看一下LinkedListget方法的源码:

/**
 * 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;
}


可以看到,首先使用checkElementIndexindex进行检验;然后点进去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;
    }
}
  1. 可以看出,当index位于size的前一半时,从前往后去遍历元素;当index位于size的后一半时,从后往前去遍历元素;所以当index恰好位于中间位置时,所需时间最长;
  2. 测试一下使用get方法来获取中间元素所需的时间:
long startTime = System.currentTimeMillis();
list.get(max / 2).hashCode();
long endTime = System.currentTimeMillis();
System.out.println("LinkedList获取中间元素所需时间: "+(endTime - startTime));


运行结果如下:

LinkedList获取中间元素所需时间: 17

发现该方法运行时间较长,因为需要遍历元素去寻找数据;


结论

  1. 对于需要经常查询元素的场景,应尽量使用ArrayList来实现,所需时间复杂度为O(1)
目录
打赏
0
0
0
0
6
分享
相关文章
每日一道面试题之LinkedList VS ArrayList~
每日一道面试题之LinkedList VS ArrayList~
ArrayList和LinkedList的区别
ArratList的底层使用动态数组,默认容量为10,当元素数量到达容量时,生成一个新的数组,大小为前一次的1.5倍,然后将原来的数组copy过来; 因为数组有索引,所以ArrayList查找数据更快,但是添加数据效率更低 LinkedList的底层使用链表,在内存中是离散的,没有扩容机制;LinkedList在查找数据时需要从头遍历,所以查找慢,但是添加数据效率更高
每日一道面试题之ArrayList 和 LinkedList 的区别是什么?
每日一道面试题之ArrayList 和 LinkedList 的区别是什么?
ArrayList 和 LinkedList 的区别【重要】
ArrayList 和 LinkedList 的区别【重要】
83 0
ArrayList 和 LinkedList 的区别
ArrayList 和 LinkedList 的区别
|
9月前
面试题之:ArrayList和LinkedList有哪些区别
面试题之:ArrayList和LinkedList有哪些区别
Java集合框架:ArrayList和LinkedList的区别是什么?
Java集合框架:ArrayList和LinkedList的区别是什么?
79 0
集合框架之ArrayList和LinkedList的区别
集合框架之ArrayList和LinkedList的区别
81 0
一文带你进行ArrayList 源码分析
一文带你进行ArrayList 源码分析
10904 1

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等