各自特性:
ArrayList : 是一由连续的内存块组成的数组,范围大小可变的,当不够时增加为原来1.5倍大小,数组。 :调用trimToSize方法,使得存储区域的大小调整为当前元素数量所需要的空间大小,垃圾回收器将会回收多余存储空间。
LinkedList
: 是由随机内存块通过指针连接起来的,范围大小可变的,当不够时增加为原来2倍大小,一个双向链表,
书上得来: 结论一 : ArrayList集合访问查找比LinkedList集合速度快,
结论二 : LinkedList集合增删元素比ArrayList集合速度快。
原因:
ArrayList是连续的内存地址,访问时根据下标,即与首地址的偏移量来确定元素;LinkedList集合是随机内存地址,查找时需要遍历链表,如果需要查找元素位于链表末尾,也只有老老实实把前面的挨个儿查看比较一番,所以有了结论一。
ArrayList集合每删除一个元素,GC会回收没有引用的那块内存地址,所以删除一个元素整个的集合元素要移动一下位置,那么如果删除了第一个元素,很明显需要把后面的n-1个元素向前移动一个单位;LinkedList集合的随机内存地址数据结构使得每增加和删除一个元素,只需要修改前后两个元素的头尾指针即可,所以有了结论二。
看上去上面说的很有道理。。但是。。。这是真的吗??????
动手试一试,你会发现神奇之处!!!
public class Collection_ArrayListPK_LinkedList {
public static void main(String[] args)
{
// 测试访问比较
TestLook();
// 测试添加
// TestAdd();
public static void TestLook()
{
long [] timeBegin = new long[2];
long [] timeEnd = new long[2];
Integer [] ia = new Integer[5000];
for(int i=0;i<ia.length;i++)
ia[i] = i ;
Random rm = new Random();
int temp ;
/**
* 测试对大小为5000 的ArrayList进行100000次随机访问所用时间
*/
List alist = new ArrayList(Arrays.asList(ia));
timeBegin[0] = System.currentTimeMillis();
for(int k=0;k<100000;k++)
{
temp = (Integer)alist.get(rm.nextInt(5000));
}
timeEnd[0] = System.currentTimeMillis();
/**
* 测试对大小为5000 的LinkedList进行100000次随机访问所用时间
*/
List llist = new LinkedList(Arrays.asList(ia));
timeBegin[1] = System.currentTimeMillis();
for(int k=0;k<100000;k++)
{
temp = (Integer)llist.get(rm.nextInt(5000));
}
timeEnd[1] = System.currentTimeMillis();
System.out.println("+++分析对大小为5000 的LinkedList类型和ArrayList类型进行的100000次随机访问所用时间比较+++");
System.out.println("ArrayList的100000次随机访问速度测试:");
System.out.println("耗时:"+(timeEnd[0]-timeBegin[0])+"ms");
System.out.println("LinkedList的100000次随机访问速度测试");
System.out.println("耗时:"+(timeEnd[1]-timeBegin[1])+"ms");
/*
* 随机修改测试
*/
}
证明第一条结论:
public static void TestAdd()
{
long [] timeBegin = new long[2];
long [] timeEnd = new long[2];
Integer [] ia = new Integer[5000];
Random rm = new Random();
int temp ;
/**
* 测试对大小为5000 的ArrayList进行100000次随机访问所用时间
*/
List alist = new ArrayList(Arrays.asList(ia));
timeBegin[0] = System.currentTimeMillis();
for(int k=0;k<10000;k++)
{
alist.add(rm.nextInt(5000), 1);
}
timeEnd[0] = System.currentTimeMillis();
List llist = new LinkedList(Arrays.asList(ia));
timeBegin[1] = System.currentTimeMillis();
for(int k=0;k<10000;k++)
{
llist.add(rm.nextInt(5000), 1);
}
timeEnd[1] = System.currentTimeMillis();
System.out.println("+++分析对大小为5000 的LinkedList类型和ArrayList类型进行的10000次随机添加所用时间比较+++");
System.out.println("ArrayList的10000次添加速度测试:");
System.out.println("耗时:"+(timeEnd[0]-timeBegin[0])+"ms");
System.out.println("LinkedList的10000次添加速度测试");
System.out.println("耗时:"+(timeEnd[1]-timeBegin[1])+"ms");
}
public static void TestAdd()
{
long [] timeBegin = new long[10];
long [] timeEnd = new long[10];
Integer [] ia = new Integer[5000];
Random rm = new Random();
int temp ;
/**
* 测试对大小为5000 的ArrayList进行100000次随机访问所用时间
*/
List alist = new ArrayList(Arrays.asList(ia));
// 100
timeBegin[0] = System.currentTimeMillis();
for(int k=0;k<100;k++)
{
alist.add(rm.nextInt(5000), 1);
}
timeEnd[0] = System.currentTimeMillis();
// 1000
timeBegin[1] = System.currentTimeMillis();
for(int k=0;k<1000;k++)
{
alist.add(rm.nextInt(5000), 1);
}
timeEnd[1] = System.currentTimeMillis();
//10000
timeBegin[2] = System.currentTimeMillis();
for(int k=0;k<10000;k++)
{
alist.add(rm.nextInt(5000), 1);
}
timeEnd[2] = System.currentTimeMillis();
// 50000
timeBegin[6] = System.currentTimeMillis();
for(int k=0;k<50000;k++)
{
alist.add(rm.nextInt(5000), 1);
}
timeEnd[6] = System.currentTimeMillis();
// 100000
timeBegin[8] = System.currentTimeMillis();
for(int k=0;k<100000;k++)
{
alist.add(rm.nextInt(5000), 1);
}
timeEnd[8] = System.currentTimeMillis();
List llist = new LinkedList(Arrays.asList(ia));
// 100
timeBegin[3] = System.currentTimeMillis();
for(int k=0;k<100;k++)
{
llist.add(rm.nextInt(5000), 1);
}
timeEnd[3] = System.currentTimeMillis();
// 1000
timeBegin[4] = System.currentTimeMillis();
for(int k=0;k<1000;k++)
{
llist.add(rm.nextInt(5000), 1);
}
timeEnd[4] = System.currentTimeMillis();
// 10000
timeBegin[5] = System.currentTimeMillis();
for(int k=0;k<10000;k++)
{
llist.add(rm.nextInt(5000), 1);
}
timeEnd[5] = System.currentTimeMillis();
// 50000
timeBegin[7] = System.currentTimeMillis();
for(int k=0;k<50000;k++)
{
llist.add(rm.nextInt(5000), 1);
}
timeEnd[7] = System.currentTimeMillis();
// 100000
timeBegin[9] = System.currentTimeMillis();
for(int k=0;k<100000;k++)
{
llist.add(rm.nextInt(5000), 1);
}
timeEnd[9] = System.currentTimeMillis();
System.out.println("+++分析对大小为5000 的LinkedList类型和ArrayList类型进行的10000次随机添加所用时间比较+++");
System.out.println("ArrayList的添加速度测试:");
System.out.println("100次");
System.out.println("ArrayList -耗时:"+(timeEnd[0]-timeBegin[0])+"ms");
System.out.println("LinkedList -耗时:"+(timeEnd[3]-timeBegin[3])+"ms");
System.out.println("1000次");
System.out.println("ArrayList -耗时:"+(timeEnd[1]-timeBegin[1])+"ms");
System.out.println("LinkedList -耗时:"+(timeEnd[4]-timeBegin[4])+"ms");
System.out.println("10000次");
System.out.println("ArrayList -耗时:"+(timeEnd[2]-timeBegin[2])+"ms");
System.out.println("LinkedList -耗时:"+(timeEnd[5]-timeBegin[5])+"ms");
System.out.println("50000次");
System.out.println("ArrayList -耗时:"+(timeEnd[6]-timeBegin[6])+"ms");
System.out.println("LinkedList -耗时:"+(timeEnd[7]-timeBegin[7])+"ms");
System.out.println("100000次");
System.out.println("ArrayList -耗时:"+(timeEnd[8]-timeBegin[8])+"ms");
System.out.println("LinkedList -耗时:"+(timeEnd[9]-timeBegin[9])+"ms");
}
结果一:
+++分析对大小为5000 的LinkedList类型和ArrayList类型进行的100000次随机访问所用时间比较+++
ArrayList的100000次随机访问速度测试:
耗时:27ms
LinkedList的100000次随机访问速度测试
耗时:225ms
补充: 多次测试证明第一条结论是对的。。。这哈踏实了。
结果二:
+++分析对大小为5000 的LinkedList类型和ArrayList类型进行的10000次随机添加所用时间比较+++
ArrayList的添加速度测试:
100次
ArrayList -耗时:2ms
LinkedList -耗时:3ms
1000次
ArrayList -耗时:2ms
LinkedList -耗时:5ms
10000次
ArrayList -耗时:46ms
LinkedList -耗时:91ms
50000次
有没有发现新大陆的感觉???
ArrayList -耗时:517ms
LinkedList -耗时:708ms
100000次
ArrayList -耗时:4316ms
LinkedList -耗时:1455ms
补充: 看来第二个结论有点问题咯。。说得太绝对了。。。
分析后者原因: 得出第二个结论主要是考虑增删元素的处理的时间去了,忽略了一个问题,就是查找到该元素的时间,ArrayList 集合
虽然处理慢,但是查找很快。。有优势的也有弱势之处,当优势和弱势比例不同自然而然结果就不一样。。所以看到新大陆了是应该的。。。
- java集合(ArrayList,Vector,LinkedList,HashSet,TreeSet的功能详解)
说起集合,我们会潜意识里想到另外一个与之相近的名词——数组,OK!两者确实有相似之处,但也正是这点才是我们应该注意的地方,下面简单列出了两者的区别(具体功能的不同学习这篇文章后就会明白了): 数组 长 ...
- 集合框架-ArrayList,Vector,Linkedlist
// ClassCastException 报错,注意,千万要搞清楚类型 * Vector的特有功能: * 1:添加功能 * public void addElement(Object obj) -- ...
- Java集合(六)--ArrayList、LinkedList和Vector对比
在前两篇博客,学习了ArrayList和LinkedList的源码,地址在这: Java集合(五)--LinkedList源码解读 Java集合(四)--基于JDK1.8的ArrayList源码解读 ...
- 毫不留情地揭开 ArrayList 和 LinkedList 之间的神秘面纱
先看再点赞,给自己一点思考的时间,思考过后请毫不犹豫微信搜索[沉默王二],关注这个靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有技术大佬整理的面试题, ...
- Java集合之ArrayList和LinkedList的实现原理以及Iterator详解
ArrayList实现可变数组的原理: 当元素超出数组内容,会产生一个新数组,将原来数组的数据复制到新数组中,再将新的元素添加到新数组中. ArrayList:是按照原数组的50%来延长,构造一个初始 ...
- java List集合记录 ArrayList和LinkedList的区别
一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问 ...
- Java自学-集合框架 ArrayList和LinkedList的区别
ArrayList和LinkedList的区别 步骤 1 : ArrayList和LinkedList的区别 ArrayList ,插入,删除数据慢 LinkedList, 插入,删除数据快 Arra ...
- 集合总结--ArrayList、LinkedList、HashMap
一.概述 ArrayList:数组集合. 查询.修改.新增(尾部新增)快,删除.新增(队列中间)慢,适用于查询.修改较多的场景. LinkedList:双向链表集合.查 ...
- 集合引入(ArrayList、LinkedList)
1.引入 代替数组固定大小操作不变 2.ArrayList 常用的操作(add,remove) 3.LinkedList 能实现一些特殊的操作(pop)
随机推荐
- python-Web-flask-蓝图和单元测试
4 蓝图和单元测试: 能够使用代码实现蓝图对项目进行模块化 admin=Blueprint('admin',__name__) # 创建一个蓝图对象 @admin.route('/') def adm ...
- elasticsearch查询操作
#查看节点信息 curl -X GET http://localhost:9200/_nodes #打开文件数信息 curl -X GET http://localhost:9200/_nodes/s ...
- JQuery Validate - 自定义js验证
(function (window, $) { var validResult = {}; var checkObjs = { /** * 检查输入的一串字符是否全部是数字 * 输入:str Stri ...
- HIVE配置mysql metastore
HIVE配置mysql metastore hive中除了保存真正的数据以外还要额外保存用来描述库.表.数据的数据,称为hive的元数据.这些元数据又存放在何处呢? 如果不修改配置hive ...
- elasticsearch基本概念理解+elasticsearch 的shards unassigned处理方法 -- 最佳运维实践 - 集群规划
1.es与MySQL的概念对比 2.概念理解 2.1 Index : 一个索引即是文档的集合 2.2 Document : 一个文档即是一个可被索引的基础单元信息,一条记录: 2.3 Replicas ...
- 环信联合创始人: Saas敏捷开发实践!
马晓宇 --环信联合创始人/执行总裁 我们是一个做云服务的创业公司,所以我就云服务创业公司的角度,来谈谈我们是怎么去实践敏捷开发的.确切地说,就是讲讲我们这几年的这些教训... 1-创业公司敏捷开发流 ...
- 怎样禁用浏览器的Cookie功能
使用: window.navigator.cookieEnabled; window.navigator.cookieEnabled = true; 这样设置以后, 浏览器就不会接受和保存服务器传过来 ...
- Your ApplicationContext is unlikely tostart due to a @ComponentScan of the defau
一.错误提示: Your ApplicationContext is unlikely tostart due to a @ComponentScan of the default package.. ...
- C语言两个特别大的整数类型相加超出范围使用两个技巧
技巧1:用long (%ld)或者long long(%lld)类型存取 技巧2:当两个同号的数字相加,放到等号的另一边,变成减号 问题: 给定区间[-2的31次方, 2的31次方]内的3个整数A.B ...
- MySQL修改和查看表类型
//修改表类型alter table verify_code engine = MEMORY;//查看表类型show create table verify_code;