各自特性:
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)
随机推荐
- unity3d 触屏多点触控(旋转与缩放)
unity3d 触屏多点触控(旋转与缩放) /*Touch OrbitProgrammed by: Randal J. Phillips (Caliber Mengsk)Original Creati ...
- Flutter Window环境运行(VSCode + 单独运行Android 虚拟机)
官网以及很多网上文章的开发都是基于Android ,因为它能创建不同类型移动设备虚拟机.但个人始终觉得它太庞大,启动慢耗资源,但我们使用Flutter又离不开虚拟机. 经过实践,现在能成功的单独启动移 ...
- JavaScript 检测值
了解常见的真值和假值,可以增强判断能力.在使用if判断时,提升编码速度. 了解常见的检测和存在,一样可以增强判断能力,而且是必须掌握的. 数组和对象被视为真值 1 2 3 4 5 6 7 8 9 10 ...
- A+B Problem Plus and A-B Problem Plus and A*B Problem Plus
//we have defined the necessary header files here for this problem. //If additional header files are ...
- 粒子群优化算法(PSO)的基本概念
介绍了PSO基本概念,以及和遗传算法的区别: 粒子群算法(PSO)Matlab实现(两种解法)
- jumpserver0.4.0与python3版本安装
环境: 系统:CentOS 6.5 Python版本:Python3.6 安装目录:/Data/apps/ 一. 环境准备: 1. 基本工具库: # yum -y install sqlite-de ...
- PTA(Advanced Level)1067.Sort with Swap(0, i)
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...
- 记录一次线上yarn RM频繁切换的故障
周末一大早被报警惊醒,rm频繁切换 急急忙忙排查 看到两处错误日志 错误信息1 ervation <memory:0, vCores:0> 2019-12-21 11:51:57,781 ...
- Ubuntu 安装 QtCreator (version : Qt 5.9.8)
平台 :Ubuntu 16.04 QT :5.9.8 (open source) 首先去QT安装包下载安装包,为了保持与arm板子的统一,本人选择了 5.9.8 版本的QT 可 ...
- Codeforces 1247C. p-binary
传送门 首先 $n=\sum_{i=1}^{ans}(2^{x_{ans}}+p)$ 可以变成 $n-ans \cdot p=\sum_{i=1}^{ans}2^{x_{ans}}$ 注意到如果 $n ...