前面已经学习完了List部分的源码,主要是ArrayList和LinkedList两部分内容,这一节主要总结下List部分的内容。

List概括

先来回顾一下List在Collection中的的框架图:

从图中我们可以看出:

1. List是一个接口,它继承与Collection接口,代表有序的队列。

2. AbstractList是一个抽象类,它继承与AbstractCollection。AbstractList实现了List接口中除了size()、get(int location)之外的方法。

3. AbstractSequentialList是一个抽象类,它继承与AbstrctList。AbstractSequentialList实现了“链表中,根据index索引值操作链表的全部方法”。

4. ArrayList、LinkedList、Vector和Stack是List的四个实现类,其中Vector是基于JDK1.0,虽然实现了同步,但是效率低,已经不用了,Stack继承与Vector,所以不再赘述。

5. LinkedList是个双向链表,它同样可以被当作栈、队列或双端队列来使用。

ArrayList和LinkedList区别

我们知道,通常情况下,ArrayList和LinkedList的区别有以下几点:

1. ArrayList是实现了基于动态数组的数据结构,而LinkedList是基于链表的数据结构;

2. 对于随机访问get和set,ArrayList要优于LinkedList,因为LinkedList要移动指针;

3. 对于添加和删除操作add和remove,一般大家都会说LinkedList要比ArrayList快,因为ArrayList要移动数据。但是实际情况并非这样,对于添加或删除,LinkedList和ArrayList并不能明确说明谁快谁慢,下面会详细分析。

我们结合之前分析的源码,来看看为什么是这样的:

ArrayList中的随机访问、添加和删除部分源码如下:

//获取index位置的元素值
public E get(int index) {  rangeCheck(index); //首先判断index的范围是否合法  return elementData(index);
}  //将index位置的值设为element,并返回原来的值
public E set(int index, E element) {  rangeCheck(index);  E oldValue = elementData(index);  elementData[index] = element;  return oldValue;
}  //将element添加到ArrayList的指定位置
public void add(int index, E element) {  rangeCheckForAdd(index);  ensureCapacityInternal(size + 1);  // Increments modCount!!  //将index以及index之后的数据复制到index+1的位置往后,即从index开始向后挪了一位  System.arraycopy(elementData, index, elementData, index + 1,  size - index);   elementData[index] = element; //然后在index处插入element  size++;
}  //删除ArrayList指定位置的元素
public E remove(int index) {  rangeCheck(index);  modCount++;  E oldValue = elementData(index);  int numMoved = size - index - 1;  if (numMoved > 0)  //向左挪一位,index位置原来的数据已经被覆盖了  System.arraycopy(elementData, index+1, elementData, index,  numMoved);  //多出来的最后一位删掉  elementData[--size] = null; // clear to let GC do its work  return oldValue;
}  

LinkedList中的随机访问、添加和删除部分源码如下:

//获得第index个节点的值
public E get(int index) {  checkElementIndex(index);  return node(index).item;
}  //设置第index元素的值
public E set(int index, E element) {  checkElementIndex(index);  Node<E> x = node(index);  E oldVal = x.item;  x.item = element;  return oldVal;
}  //在index个节点之前添加新的节点
public void add(int index, E element) {  checkPositionIndex(index);  if (index == size)  linkLast(element);  else  linkBefore(element, node(index));
}  //删除第index个节点
public E remove(int index) {  checkElementIndex(index);  return unlink(node(index));
}  //定位index处的节点
Node<E> node(int index) {  // assert isElementIndex(index);  //index<size/2时,从头开始找  if (index < (size >> 1)) {  Node<E> x = first;  for (int i = 0; i < index; i++)  x = x.next;  return x;  } else { //index>=size/2时,从尾开始找  Node<E> x = last;  for (int i = size - 1; i > index; i--)  x = x.prev;  return x;  }
}  

从源码可以看出,ArrayList想要get(int index)元素时,直接返回index位置上的元素,而LinkedList需要通过for循环进行查找,虽然LinkedList已经在查找方法上做了优化,比如index < size / 2,则从左边开始查找,反之从右边开始查找,但是还是比ArrayList要慢。这点是毋庸置疑的。
        ArrayList想要在指定位置插入或删除元素时,主要耗时的是System.arraycopy动作,会移动index后面所有的元素;LinkedList主耗时的是要先通过for循环找到index,然后直接插入或删除。这就导致了两者并非一定谁快谁慢,下面通过一个测试程序来测试一下两者插入的速度:

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/* * @description 测试ArrayList和LinkedList插入的效率 * @eson_15      */
public class ArrayOrLinked {    static List<Integer> array=new ArrayList<Integer>();    static List<Integer> linked=new LinkedList<Integer>();    public static void main(String[] args) {    //首先分别给两者插入10000条数据  for(int i=0;i<10000;i++){    array.add(i);    linked.add(i);    }    //获得两者随机访问的时间  System.out.println("array time:"+getTime(array));    System.out.println("linked time:"+getTime(linked));    //获得两者插入数据的时间  System.out.println("array insert time:"+insertTime(array));    System.out.println("linked insert time:"+insertTime(linked));    }    public static long getTime(List<Integer> list){    long time=System.currentTimeMillis();    for(int i = 0; i < 10000; i++){    int index = Collections.binarySearch(list, list.get(i));    if(index != i){    System.out.println("ERROR!");    }    }    return System.currentTimeMillis()-time;    }    //插入数据  public static long insertTime(List<Integer> list){   /* * 插入的数据量和插入的位置是决定两者性能的主要方面, * 我们可以通过修改这两个数据,来测试两者的性能 */  long num = 10000; //表示要插入的数据量  int index = 1000; //表示从哪个位置插入  long time=System.currentTimeMillis();    for(int i = 1; i < num; i++){    list.add(index, i);       }    return System.currentTimeMillis()-time;    }    }    

主要有两个因素决定他们的效率,插入的数据量和插入的位置。我们可以在程序里改变这两个因素来测试它们的效率。

当数据量较小时,测试程序中,大约小于30的时候,两者效率差不多,没有显著区别;当数据量较大时,大约在容量的1/10处开始,LinkedList的效率就开始没有ArrayList效率高了,特别到一半以及后半的位置插入时,LinkedList效率明显要低于ArrayList,而且数据量越大,越明显。比如我测试了一种情况,在index=1000的位置(容量的1/10)插入10000条数据和在index=5000的位置以及在index=9000的位置插入10000条数据的运行时间如下:

在index=1000出插入结果:
array time:4
linked time:240
array insert time:20
linked insert time:18  在index=5000处插入结果:
array time:4
linked time:229
array insert time:13
linked insert time:90  在index=9000处插入结果:
array time:4
linked time:237
array insert time:7
linked insert time:92  

从运行结果看,LinkedList的效率是越来越差。

所以当插入的数据量很小时,两者区别不太大,当插入的数据量大时,大约在容量的1/10之前,LinkedList会优于ArrayList,在其后就劣与ArrayList,且越靠近后面越差。所以个人觉得,一般首选用ArrayList,由于LinkedList可以实现栈、队列以及双端队列等数据结构,所以当特定需要时候,使用LinkedList,当然咯,数据量小的时候,两者差不多,视具体情况去选择使用;当数据量大的时候,如果只需要在靠前的部分插入或删除数据,那也可以选用LinkedList,反之选择ArrayList反而效率更高。

转载于:https://www.cnblogs.com/shanheyongmu/p/6439202.html

java集合框架05——ArrayList和LinkedList的区别相关推荐

  1. java集合框架之ArrayList与LinkedList的区别

    参考http://how2j.cn/k/collection/collection-arraylist-vs-linkedlist/690.html#nowhere ArrayList和LinkedL ...

  2. 【重难点】【Java集合 03】ArrayList、LinkedList、 Vector 和 Stack 的区别、CopyOnWriteArrayList

    [重难点][Java集合 03]ArrayList.LinkedList 和 Vector 的区别.util 包下的 List.CopyOnWriteArrayList 文章目录 [重难点][Java ...

  3. 【Java集合框架】ArrayList类方法简明解析(举例说明)

    本文目录 1.API与Java集合框架 2.ArrayList类方法解析 2.1 add() 2.2 addAll() 2.3 clear() 2.4 clone() 2.5 contains() 2 ...

  4. Java集合框架:ArrayList

    欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 欢迎跳转到本文的原文链接:https: ...

  5. 14 Java集合(集合框架+泛型+ArrayList类+LinkedList类+Vector类+HashSet类等)

    本篇主要是集合框架基础和List集合,Map集合等等后续更 集合 14.1 集合框架 14.1.1 概念 14.1.2 集合架构 14.2 Collection接口 14.2.1 常用方法 14.3 ...

  6. 集合框架及ArrayList、LinkedList源码的个人理解

    目录 一.框架 二.集合 三.Collection集合 四.List集合 4.1 List子接口 4.2 数组与链表 4.3 ArrayList[重点] 4.4 LinkedList 4.5 Vect ...

  7. java集合框架02——ArrayList和源码分析

    上一章学习了Collection的架构,并阅读了部分源码,这一章开始,我们将对Collection的具体实现进行详细学习.首先学习List.而ArrayList又是List中最为常用的,因此本章先学习 ...

  8. java集合的添加方法_深入理解java集合框架之---------Arraylist集合 -----添加方法

    Arraylist集合 -----添加方法 1.add(E e) 向集合中添加元素 /** * 检查数组容量是否够用 * @param minCapacity */ public void ensur ...

  9. 深入理解java集合框架之---------Arraylist集合 -----添加方法

    Arraylist集合 -----添加方法 1.add(E e) 向集合中添加元素 /*** 检查数组容量是否够用* @param minCapacity*/public void ensureCap ...

最新文章

  1. Fertility of Soils:根系C P计量比影响水稻残根周际酶活的时空动态分布特征
  2. 如何在Python中捕获SIGINT?
  3. 为预测用户出行需求,ofo开始使用AI实现智能调度
  4. 数据结构与算法09 之图
  5. 前端开发总结--之关于FusionSphere WEBUI的想法
  6. aes key长度_AES加密(1): 基本AES算法
  7. 想要成为算法工程师,需要具备开发能力?-开课吧
  8. JdbcTemplate 的使用
  9. 神经网络入门--学习资源
  10. Proteus器件查找
  11. imac 蓝牙机械键盘_最好的蓝牙机械键盘
  12. Micrium DEMO9S12NE64 uCOS-II 官方包 调试记录!
  13. 队列的图文介绍及C/C++的实现实例(转自http://www.cnblogs.com/skywang12345/p/3562279.html)
  14. 详解机器学习/深度学习中的梯度消失/梯度爆炸的原因/解决方案
  15. 记录python量化投资学习过程(二)- 常见指标以及概念的记录
  16. thinkphp使用编辑器kindeditor
  17. 解决阿里云服务器被恶意挖矿问题
  18. 图解卷积前后图像尺寸的关系
  19. Jim Keller:在指令集上辩论是一件悲哀的事情
  20. 圆形Shape输出面积和周长

热门文章

  1. 企业级应用架构(三)三层架构之数据访问层的改进以及测试DOM的发布
  2. 华中地区高校第七届ACM程序设计大赛——之字形矩阵【2012年5月27日】
  3. ms sql server 添加列,删除列。
  4. 《netty入门与实战》笔记-02:服务端启动流程
  5. 算法之美 : 位运算
  6. amazeui学习笔记--css(基本样式3)--文字排版Typography
  7. Android控制ScrollView滑动速度
  8. Object Pools 喷泉效果实现
  9. 谢烟客---------Linux之DNS服务系统的基础知识
  10. 基于Linux的 Open×××网络之网络架构应用实例