一,线程安全性

Vector、Stack:线程安全

ArrayList、LinkedList:非线程安全

二,实现方式

LinkedList:双向链表

ArrayList,Vector,Stack:数组

三,容量扩展方面

由于ArrayList和Vector(Stack继承自Vector,只在Vector的基础上添加了几个Stack相关的方法,故之后不再对Stack做特别的说明)使用数组实现,当数组长度不够时,其内部会创建一个更大的数组,然后将原数组中的数据拷贝至新数组中

//ArrayList
public boolean add(E e) {
ensureCapacity(size + 1);
elementData[size++] = e;
return true;
} public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
//如果这次扩展不能满足要求,那就直接用minCapacity
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
}

如需扩展,则每次至少扩展至(原长度*3)/2 + 1

//Vector
public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
} private void ensureCapacityHelper(int minCapacity) {
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object[] oldData = elementData;
int newCapacity = (capacityIncrement > 0) ?
(oldCapacity + capacityIncrement) : (oldCapacity * 2);
//如果这次扩展不能满足要求,那就直接用minCapacity
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
elementData = Arrays.copyOf(elementData, newCapacity);
}
}

如果在创建Vector时不指定capacityIncrement(自动扩展长度)的值,如需扩展,则每次至少扩展至原长度的2倍

四,效率方面

这里仅仅比较ArrayList和LinkedList之间的效率差异

1,查询

ArrayList直接通过下标进行定位

//ArrayList
public E get(int index) {
RangeCheck(index);//检查下标是否超过数组长度
return (E) elementData[index];
}

LinkedList则需要进行遍历,平均遍历次数应为n/4

//LinkedList
public E get(int index) {
return entry(index).element;
} private Entry<E> entry(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
Entry<E> e = header;
//size >>1 相当于size/2
//由于LinkedList由双向链表实现,故从离得较近的一端开始遍历更快
if (index < (size >> 1)) {
for (int i = 0; i <= index; i++)
e = e.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
}
return e;
}

对于指定位置查询,由于可以通过下标直接进行定位,ArrayList的速度远快于LinkedList

但是如果都为首尾位置的查询,情况会大为不同,因为LinkedList也是可以直接定位到首尾位置的

//LinkedList
public E getFirst() {
if (size==0)
throw new NoSuchElementException();
return header.next.element;
} public E getLast() {
if (size==0)
throw new NoSuchElementException();
return header.previous.element;
}

此时ArrayList和LinkedList的效率相同

2,插入

对于ArrayList,指定位置插入有可能首先需要对数组容量进行扩展,之后还有可能导致数组中的数据需要顺次移动(代码中通过数组拷贝实现,避免了数据一个一个的移动),极端情况下插入一个数据将进行两次数组拷贝

//ArrayList
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1); //如必要,将对数组容量进行扩展
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}

如果不指定插入位置,则插入至数组末端,此时只需考虑可能的数组容量扩展对性能带来的影响

//ArrayList
public boolean add(E e) {
ensureCapacity(size + 1);
elementData[size++] = e;
return true;
}

由于LinkedList是由链表实现的,并没有指定位置插入的方法,即便如此,一切也显得如此美好

/LinkedList
public boolean add(E e) {
addBefore(e, header);
return true;
} private Entry<E> addBefore(E e, Entry<E> entry) {
Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);
newEntry.previous.next = newEntry;
newEntry.next.previous = newEntry;
size++;
modCount++;
return newEntry;
}

当然了,LinkedList可以直接将数据插入至首尾

//LinkedList
public void addFirst(E e) {
addBefore(e, header.next);
} public void addLast(E e) {
addBefore(e, header);
}

总体来说,LinkedList效率高于ArrayList,即使在末尾插入,ArrayList也需要考虑可能的容量扩展对性能带来的影响

3,修改

和查询属于同一种情况

4,删除

指定位置的删除和插入属于同一种情况

除了删除指定位置数据,ArrayList和LinkedList都包含一个clear()方法用来清除所有数据

//ArrayList
public void clear() {
modCount++; // Let gc do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
[java] view plain copy 在CODE上查看代码片派生到我的代码片
//LinkedList
public void clear() {
Entry<E> e = header.next;
while (e != header) {
Entry<E> next = e.next;
e.next = e.previous = null;
e.element = null;
e = next;
}
header.next = header.previous = header;
size = 0;
modCount++;
}

由于都需要进行遍历,故效率相同

ArrayList,LinkedList,Vector,Stack之间的区别的更多相关文章

  1. ArrayList LinkedList Vector

    ArrayList是基于数组实现的,没有容量的限制. 在删除元素的时候,并不会减少数组的容量大小,可以调用ArrayList的trimeToSize()来缩小数组的容量. ArrayList, Lin ...

  2. ArrayList, LinkedList, Vector - dudu:史上最详解

    ArrayList, LinkedList, Vector - dudu:史上最详解 我们来比较一下ArrayList, LinkedLIst和Vector它们之间的区别.BZ的JDK版本是1.7.0 ...

  3. ArrayList LinkedList Vector之间的区别

    List主要有ArrayList,LinkedList和vector三种实现.这三种都实现了List接口,使用方式也很相似,主要区别在于其实现方式的不同! 这三种数据结构中,ArrayList和Vec ...

  4. ArrayList,LinkedList,vector的区别

    1,Vector.ArrayList都是以类似数组的形式存储在内存中,LinkedList则以链表的形式进行存储. 2.List中的元素有序.允许有重复的元素,Set中的元素无序.不允许有重复元素. ...

  5. 集合类源码(二)Collection之List(ArrayList, LinkedList, Vector)

    ArrayList 功能 完全命名 public class ArrayList<E> extends AbstractList<E> implements List<E ...

  6. ArrayList,LinkedList,Vector集合的认识

    最近在温习Java集合部分,花了三天时间读完了ArrayList与LinkedList以及Vector部分的源码.之前都是停留在简单使用ArrayList的API,读完源码看完不少文章后总算是对原理方 ...

  7. java 中 ArrayList LinkedList Vector 三者的异同点

    1.ArrayList和Vector都是基于数组实现的,所以查询速度很快,增加和删除(非最后一个节点)速度慢: Vector是线程安全的,ArrayList不是. 2.LinkedList 是一个双向 ...

  8. java类集框架(ArrayList,LinkedList,Vector区别)

    主要分两个接口:collection和Map 主要分三类:集合(set).列表(List).映射(Map)1.集合:没有重复对象,没有特定排序方式2.列表:对象按索引位置排序,可以有重复对象3.映射: ...

  9. List的三个子类ArrayList,LinkedList,Vector区别

    一:List的三个子类的特点 ArrayList: 底层数据结构是数组,查询快,增删慢. 线程不安全,效率高.Vector: 底层数据结构是数组,查询快,增删慢. 线程安全,效率低.Vector相对A ...

随机推荐

  1. [转]理解与使用Javascript中的回调函数

    在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...

  2. 自己编写基于MVC的轻量级PHP框架

    做WEB开发已有三年,每次都写重复的东西, 因此,想自己写一下框架,以后开发方便.本人之前asp.NET一年开发,jsp半年,可是后来因为工作的原故换成PHP.其实很不喜欢PHP的语法.还有PHP的函 ...

  3. Android编程: ViewPager和Dialogs组件

    学习内容:ViewPager和Dialogs组件 ====ViewPager组件==== 它的作用主要是支持屏幕左右滑动切换列表元素,使用方式如下: 1.首先定义ID号信息,创建res/values/ ...

  4. Android实现Button事件的处理

    Android实现Button事件的处理 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 首先是最基本的线性布局,给每个控件设立id值,以供代 ...

  5. 文件读写 swift

    // // ViewController.swift // 文件读写 // // Created by mac on 15/7/12. // Copyright (c) 2015年 fangyuhao ...

  6. vc++编程之在程序中加入网址链接

    在vc++对话框编程中,我们处于某种需要(介绍自己的软件或者自己的博客)可以在对话框上增加一个网址链接,用户只要一点击,就进入了相应的网页,我在此演示下如何完成. 1 打开编译器,我们新建一个基于对话 ...

  7. Linux Shell常用技巧(目录)

    Linux Shell常用技巧(一) http://www.cnblogs.com/stephen-liu74/archive/2011/11/10/2240461.html一. 特殊文件: /dev ...

  8. Java程序员面试中的多线程问题

    很多核心Java面试题来源于多线程(Multi-Threading)和集合框架(Collections Framework),理解核心线程概念时,娴熟的实际经验是必需的.这篇文章收集了Java线程方面 ...

  9. Spring集成hibernate错误

    八月 25, 2016 7:55:31 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule ...

  10. IOS 打包后安装崩溃,debug正常运行

    今天遇到个奇葩问题,archive后的包安装后有一个crash,必崩的.但是调试跟踪时是好的. 为了方便调试,使用了release模式,这样不用每次都archive后安装进行测试.由于没法运行时deb ...