浅谈Vector、ArrayList、LinkedList
下图是Collection的类继承图
从图中可以看出:Vector、ArrayList、LinkedList这三者都实现了List 接口.所有使用方式也很相似,主要区别在于实现方式的不同,所以对不同的操作具有不同的效率。
ArrayList 就是动态数组,是Array的复杂版本,动态的增加和减少元素.当更多的元素加入到ArrayList中时,其大小将会动态地增长。
Vector 和ArrayList类似, 区别在于Vector是同步类(synchronized).因此,开销就比ArrayList要大。
LinkedList 是一个双链表,在添加和删除元素时具有比ArrayList更好的性能.但在get与set方面弱于ArrayList.当然,这些对比都是指数据量很大或者操作很频繁的情况下的对比。它还实现了 Queue 接口,该接口比List提供了更多的方法,包括 offer(),peek(),poll()等.
注意: 默认情况下ArrayList和Vector的初始容量都是10,所以如果可以预估数据量的话,分配一个较大的初始值属于最佳实践,这样可以减少调整大小的开销。
接下来将Vector 和ArrayList 、 ArrayList和LinkedList进行两两对比
ArrayList和Vector
先看一下构造方法
public Vector(int paramInt1, int paramInt2) //使用指定的初始容量和容量增量构造一个空的向量
public Vector(int paramInt) //使用指定初始容量其标准容量增量为零的空向量
public Vector() //使用指定的初始容量为10和容量增量为零的空向量
public Vector(Collection<? extends E> paramCollection) //构造一个包含指定 collection 中的元素的向量
public ArrayList(int paramInt) //构造一个具有指定初始容量的空列表
public ArrayList() //构造一个初始容量为10的空列表
public ArrayList(Collection<? extends E> paramCollection) //构造一个包含指定 collection 的元素的列表
Vector比Arraylist多一个构造方法,就是public Vector(int paramInt1,int paramInt2)这个构造方法,paramInt2就是容量增长,即增长因子,ArrayList中是没有的。 下面再来看看Arraylist和Vectora的add方法
public boolean add(E paramE)
{
ensureCapacityInternal(this.size + 1);
this.elementData[(this.size++)] = paramE;
return true;
}
private void ensureCapacityInternal(int paramInt)
{
if (this.elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
paramInt = Math.max(10, paramInt);
}
ensureExplicitCapacity(paramInt);
}
private void ensureExplicitCapacity(int paramInt)
{
this.modCount += 1;
if (paramInt - this.elementData.length > 0) {
grow(paramInt);
}
}
private void grow(int paramInt)
{
int i = this.elementData.length;
int j = i + (i >> 1);
if (j - paramInt < 0) {
j = paramInt;
}
if (j - 2147483639 > 0) {
j = hugeCapacity(paramInt);
}
this.elementData = Arrays.copyOf(this.elementData, j);
}
public synchronized boolean add(E paramE)
{
this.modCount += 1;
ensureCapacityHelper(this.elementCount + 1);
this.elementData[(this.elementCount++)] = paramE;
return true;
} private void ensureCapacityHelper(int paramInt)
{
if (paramInt - this.elementData.length > 0) {
grow(paramInt);
}
} private void grow(int paramInt)
{
int i = this.elementData.length;
int j = i + (this.capacityIncrement > 0 ? this.capacityIncrement : i);
if (j - paramInt < 0) {
j = paramInt;
}
if (j - 2147483639 > 0) {
j = hugeCapacity(paramInt);
}
this.elementData = Arrays.copyOf(this.elementData, j);
}
1、ArrayList在内存不够时默认是扩展50% + 1个;Vector是当增长因子>0,默认扩展增加一个增长因子,否则默认扩展1倍。
2、Vector的方法加了synchronized, 而ArrayList则没有。Vector属于线程安全级别的,但是大多数情况下不使用Vector,因为线程安全需要更大的系统开销
LinkedList和ArrayList的区别
LinkedList和ArrayList的差别主要来自于ArrayList和LinkedList数据结构的不同:
1) 因为ArrayList是基于索引(index)的数据结构,它使用索引在数组中搜索和读取数据是很快的。ArrayList获取数据的时间复杂度是O(1),但是要删除数据却是开销很大的,因为这需要重排数组中的所有数据。
2) 相对于ArrayList,LinkedList插入是更快的。因为LinkedList不像ArrayList一样,不需要改变数组的大小,也不需要在数组装满的时候要将所有的数据重新装入一个新的数组,这是ArrayList最坏的一种情况,时间复杂度是O(n),而LinkedList中插入或删除的时间复杂度仅为O(1)。ArrayList在插入数据时还需要更新索引(除了插入数组的尾部)。
3) 对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。
4) LinkedList需要更多的内存,因为ArrayList的每个索引的位置是实际的数据,而LinkedList中的每个节点中存储的是实际的数据和前后节点的位置。
|
Vector
|
ArrayList
|
LinkedList
|
实现方式 |
数组来实现
|
数组来实现
|
双向链表
|
线程安全 |
方法加了synchronized
|
|
|
效率 |
|
查询快
在尾部添加效率高
|
在头部添加 要快很多
删除数据 要快很多
|
遍历
|
|
for :1062865 ns
foreach : 2413494 ns
Iterator :1276122 ns
遍历的时候:选 for |
Iterator :1080352 ns
foreach : 2191874 ns
for :84410739 ns
遍历的时候:选 Iterator |
浅谈Vector、ArrayList、LinkedList的更多相关文章
- 浅谈Vector
浅谈Vector 在之前的文章中,我们已经说过线程不安全的ArrayList和LinkedList,今天我们来讲讲一个线程安全的列表容器,他就是Vector,他的底层和ArrayList一样使用数组来 ...
- Hashtable,HashMap,TreeMap有什么区别?Vector,ArrayList,LinkedList有什么区别?int和Integer有什么区别?
接着上篇继续更新. /*请尊重作者劳动成果,转载请标明原文链接:*/ /*https://www.cnblogs.com/jpcflyer/p/10759447.html* / 题目一:Hashtab ...
- 09 Collection,Iterator,List,listIterator,Vector,ArrayList,LinkedList,泛型,增强for,可变参数,HashSet,LinkedHashSet,TreeSet
09 Collection,Iterator,List,listIterator,Vector,ArrayList,LinkedList,泛型,增强for,可变参数,HashSet,LinkedHas ...
- java面试题(杨晓峰)---第八讲谈谈Vector,ArrayList,LinkedList有何区别?
Vector,ArrayList,LinkedList均为线性的数据结构,但是从现实方式与应用场景中又存在差别. 1 底层实现方式 ArrayList内部数组来实现,LinkedList内部采用双向链 ...
- 浅谈 Vector
目录 浅谈Vector 1.容器基本操作 2.vector 初始化 3.vector的赋值与swap 4.vector的增删改除 1.增加元素 2.访问元素 3.删除元素 4.元素的大小 浅谈Vect ...
- 浅谈Java中linkedlist和arraylist区别
在Java中,关于集合框架有这样一个体系结构: 其主要由两个接口派生而出:Collection和Map,然后再衍生出各自的一些实现类(比如Collection接口又被继承与Set和List接口,而他们 ...
- vector,arraylist, linkedlist的区别是什么
LinkedList类 LinkedList实现了List接口,允许null元素. 此外LinkedList提供额外的get,remove,insert方法在LinkedList的首部或尾部. Lin ...
- Vector,ArrayList, LinkedList的区别
1.Vector.ArrayList都是以类似数组的形式存储在内存中,LinkedList则以链表的形式进行存储. 2.List中的元素有序.允许有重复的元素,Set中的元素无序.不允许有重复元素. ...
- Vector ArrayList LinkedList
三者都实现了List接口! Vector与ArrayList:采用顺序存储的方式,但是Vector是线程安全的,ArrayList是线程不安全的,按需使用: 当存储空间不足的时候,ArrayList默 ...
随机推荐
- 【codeforces 438D】The Child and Sequence
[原题题面]传送门 [大致题意] 给定一个长度为n的非负整数序列a,你需要支持以下操作: 1:给定l,r,输出a[l]+a[l+1]+…+a[r]. 2:给定l,r,x,将a[l],a[l+1],…, ...
- SPOJ 10570 LONGCS - Longest Common Substring
思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成多组数据就有三倍经验了 代码 #include <cstdio> #inclu ...
- C语言: 简易图书管理系统
这只是一个简易的图书管理系统,虽然它有千余行代码,不过终究是个简单基本的东西. 在Linux系统下,用Vim编写,如要在Windows上运行则需要一些改动,主要是一些调用系统函数的改动.如Window ...
- poj1676
保存不完整数字可能对应的数字,注意小时<24,分钟小于59. AC代码 #include <stdio.h> #include <vector> using namesp ...
- IDEA Maven项目的Mybatis逆向工程
IDEA Maven项目的Mybatis逆向工程 1.配置.pom 如果是在多模块开发下,该文件逆向工程要生成的那个模块下的pom文件. <build> <plugins> & ...
- openssl 生成pfx
证书可以通过几种渠道获得, 可以购买, 可以使用IIS生成, 也可以使用Openssl这样的工具生成证书. 本篇文章主要介绍openssl生成pfx文件 首选去网关下载openssl 下载地址:htt ...
- ggplot
安装:install.packages("ggplot2") 加载:library(ggplot2) Plot(图)= data(数据集)+ Aesthetics(美学映射)+ G ...
- samtools 使用简述
功能如下: 1.View 主要功能讲sam文件转位bam文件. 涉及的参数: -b 输出bam格式..默认是sam文件 -h 输出的sam文件带header..默认不带 -H 仅仅输出header - ...
- 雷林鹏分享:jQuery EasyUI 数据网格 - 格式化列
jQuery EasyUI 数据网格 - 格式化列 以下实例格式化在 easyui DataGrid 里的列数据,并使用自定义列的 formatter,如果价格小于 20 就将文本变为红色. 为了格式 ...
- QSS 记录
1.border-style 属性分别有 none 定义无边框. hidden 与 "none" 相同.不过应用于表时除外,对于表,hidden 用于解决边框冲突. dotted ...