ArrayList与LinkedList比较
ArrayList与LinkedList比较
1.实现方式
ArrayList内部结构为数组,定义如下:
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
* will be expanded to DEFAULT_CAPACITY when the first element is added.
*/
transient Object[] elementData; // non-private to simplify nested class access
LinkedList内部结构为双向循环链表,定义如下:
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
// Node节点定义
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
2.使用场景
ArrayList适用于随机访问
LinkedList适用于于随机位置增加、删除
3.插入删除
ArrayList在插入删除时需要移动index后面的所有元素
LinkedList在插入删除时只需遍历,不需要移动元素
4.随机访问
ArrayList支持通过下标访问元素,效率高
LinkedList每次访问通过头尾遍历,效率低
5.空间占用
ArrayList因为有扩容操作,在尾部预留有额外空间,每次扩容为150%,造成一定的空间浪费(初始大小为10)
LinkedList虽然没有浪费空间,但是每个元素都存储在Node对象中,占用空间比ArrayList大
6.遍历方式
ArrayList可以使用for循环,forEach,iterator
LinkedList一般使用forEach,iterator
7.继承接口
ArrayList继承了RandomAccess接口,RandomAccess是一个标记接口,用于标明实现该接口的List支持快速随机访问,主要目的是使算法能够在随机和顺序访问的List中性能更加高效(在Collections二分查找时)。如果集合类实现了RandomAccess,则尽量用for循环来遍历,没有实现则用Iterator进行遍历。
LinkedList继承了Deque接口,便于实现栈和队列
8.性能分析
| 操作 | ArrayList | LinkedList |
|---|---|---|
| get(index) | O(1) | O(n) |
| add() | O(1) | O(1) |
| add(index) | O(n) | O(n) |
| remove() | O(n) | O(n) |
可以发现,LinkedList的add(index),和remove()的复杂度也是O(n),与ArrayList并没有差别,这是因为在增删之前需要先得到增删元素的位置,然后才能进行增删,然而LinkedList只能通过遍历来得到位置,因此复杂度为O(n),并不是O(1)。
- 末端插入,虽然二者都是O(1),但是LinkedList每次插入都要new一个对象。因此,当数据量小时,LinkedList速度快,随着数据量的增加,ArrayList速度更快。
- 随机插入
LinkedList对于插入有一个优化:当插入位置小于(size/2)时从头遍历,当插入位置大于(size/2)时,从尾遍历。
2.1 在前半段随机插入,一般来说,此时的LinkedList效率高于ArrayList。
2.2 在后半段随机插入,此时很难判断,因为在后半段,ArrayList的copy()消耗减少,而对于LinkedList来说效率不变,因此二者的性能相差不大。
代码验证,使用一个大小为1000000的List,向其中插入500000条数据,验证在不同插入位置List的性能
| 耗时 | ArrayList | LinkedList |
|---|---|---|
| 插入位置:末尾 | 0.034s | 0.034s |
| 插入位置:999999 | 17.166s | 447.135s |
| 插入位置:500001 | 120.862s | 1024.761s |
| 插入位置:1 | 307.608s | 0.045s |
| 插入位置:0 | 381.185s | 0.039s |
| 插入位置:250000 | 240.943s | 621.257s |
| 插入位置:0,2,4,6... | 63.837s | 692.319s |
总结:只有当频繁在List前端位置进行增删操作,才选用LinkedList。一般情况,都选用ArrayList。
测试代码:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Test
*/
public class Test {
//在末端插入
public static void addTest(List list, int num) {
long startTime = System.currentTimeMillis();
for(int i=0; i<num; i++) {
int a = (int) Math.round(Math.random()*num);
list.add(a);
}
long endTime = System.currentTimeMillis();
System.out.println("addTime: " + (endTime-startTime)/1000.0 + "s size:" + list.size());
}
// 在指定位置插入
public static void insertTest(List list, int num, int index) {
long totalTime=0;
long startTime=0;
long endTime=0;
startTime = System.currentTimeMillis();
for(int i=0; i<num; i++) {
int a = (int) Math.round(Math.random()*num);
startTime = System.currentTimeMillis();
list.add(index, a);
endTime = System.currentTimeMillis();
totalTime += (endTime-startTime);
}
System.out.println("insertTime: " + (totalTime)/1000.0 + "s size:" + list.size());
}
// 间隔插入
public static void insertTest(List list, int num) {
long totalTime=0;
long startTime=0;
long endTime=0;
for(int i=0; i<num; i++) {
int a = (int) Math.round(Math.random()*num);
startTime = System.currentTimeMillis();
list.add(2*i, a);
endTime = System.currentTimeMillis();
list.remove(2*i);
totalTime += (endTime-startTime);
}
System.out.println("insertTime: " + (totalTime)/1000.0 + "s size:" + list.size());
}
public static void main(String[] args) {
ArrayList<Integer> array = new ArrayList<Integer>();
LinkedList<Integer> link = new LinkedList<Integer>();
int num = 1000000;
int index = 250000;
//Test.addTest(link, num);
//Test.addTest(link, 500000);
//Test.insertTest(link, 500000, index);
//Test.insertTest(link, 500000);
Test.addTest(array, num);
//Test.addTest(array, 500000);
//Test.insertTest(array, 500000, index);
Test.insertTest(array, 500000);
}
}
ArrayList与LinkedList比较的更多相关文章
- 深入理解java中的ArrayList和LinkedList
杂谈最基本数据结构--"线性表": 表结构是一种最基本的数据结构,最常见的实现是数组,几乎在每个程序每一种开发语言中都提供了数组这个顺序存储的线性表结构实现. 什么是线性表? 由0 ...
- ArrayList,Vector,LinkedList
在java.util包中定义的类集框架其核心的组成接口有如下:·Collection接口:负责保存单值的最大父接口 |-List子接口:允许保存重复元素,数据的保存顺序就是数据的增加顺序: |-Set ...
- Java数据结构之表的增删对比---ArrayList与LinkedList之一
一.Java_Collections表的实现 与c不同Java已经实现并封装了现成的表数据结构,顺序表以及链表. 1.ArrayList是基于数组的实现,因此具有的特点是:1.有索引值方便查找,对于g ...
- C++模拟实现JDK中的ArrayList和LinkedList
Java实现ArrayList和LinkedList的方式采用的是数组和链表.以下是用C++代码的模拟: 声明Collection接口: #ifndef COLLECTION_H_ #define C ...
- ArrayList与LinkedList用法与区别
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedLis ...
- ArrayList vs LinkedList vs Vector
List概览 List,正如它的名字,表明其是有顺序的.当讨论List的时候,最好拿它跟Set作比较,Set中的元素是无序且唯一:下面是一张类层次结构图,从这张图中,我们可以大致了解java集合类的整 ...
- ArrayList 和 LinkedList 的区别
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.2.对于随机访问get和set,ArrayList优于LinkedList,因为LinkedList要移动 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)
主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...
- 集合中list、ArrayList、LinkedList、Vector的区别、Collection接口的共性方法以及数据结构的总结
List (链表|线性表) 特点: 接口,可存放重复元素,元素存取是有序的,允许在指定位置插入元素,并通过索引来访问元素 1.创建一个用指定可视行数初始化的新滚动列表.默认情况下,不允许进行多项选择. ...
随机推荐
- 0016 CSS 背景:background
目标 理解 背景的作用 css背景图片和插入图片的区别 应用 通过css背景属性,给页面元素添加背景样式 能设置不同的背景图片位置 [插入图片,不用设置img元素的父元素.自身元素大小,即可见,但是背 ...
- 【转载】实现a元素href URL链接自动刷新或新窗口打开
又是我偶像的新文,这个小技巧的用户体验真的非常非常棒! 文章转载自 张鑫旭-鑫空间-鑫生活 http://www.zhangxinxu.com/ 原文链接:https://www.zhangxinxu ...
- 「UVA10810」Ultra-QuickSort 解题报告
题面 看不懂?! 大概的意思就是: 给出一个长度为n的序列,然后每次只能交换相邻的两个数,问最小需要几次使序列严格上升 不断读入n,直到n=0结束 思路: 交换相邻的两个数,这不就类似冒泡排序吗?但是 ...
- (二)unittst用例操作
一.跳过用例 @unittest.skip(reason) 跳过被此装饰器装饰的测试. reason 为测试被跳过的原因. 应用场景: 1,有些用例不需要再次执行,或者作废的用例 2,本次测试构建,不 ...
- 机器学习回顾篇(14):主成分分析法(PCA)
.caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...
- Java添加、读取Excel公式
操作excel表格用公式来处理数据时,可通过创建公式来运算数据,或通过读取公式来获取数据信息来源.本文以通过Java代码来演示在Excel中创建及读取公式的方法.这里使用了Excel Java类库(F ...
- Salesforce LWC学习(十一) port 1717报错的处理
使用vs code开发lwc的步骤,通常为先创建项目(create project)然后授权一个org(authorize an org),授权以后我们通常便会download代码到本地或者Uploa ...
- Eclipse中安装Jetty服务器
1. 在eclipse中安装jetty适配器 方法一: (1) 打开 Windows -> Preference -> Server -> Runtime Environment , ...
- 【Java基础总结】数据库编程
MySQL数据库查询 import java.sql.*; public class JdbcDemo1{ public static void main(String[] args){ try{ / ...
- 【一起学源码-微服务】Hystrix 源码一:Hystrix基础原理与Demo搭建
说明 原创不易,如若转载 请标明来源! 欢迎关注本人微信公众号:壹枝花算不算浪漫 更多内容也可查看本人博客:一枝花算不算浪漫 前言 前情回顾 上一个系列文章讲解了Feign的源码,主要是Feign动态 ...