java 集合系列目录:

第1部分 List概括

先回顾一下List的框架图

(01) List 是一个接口,它继承于Collection的接口。它代表着有序的队列。

(02) AbstractList 是一个抽象类,它继承于AbstractCollection。AbstractList实现List接口中除size()、get(int location)之外的函数。

(03) AbstractSequentialList 是一个抽象类,它继承于AbstractList。AbstractSequentialList 实现了“链表中,根据index索引值操作链表的全部函数”。

(04) ArrayList, LinkedList, Vector, Stack是List的4个实现类。

ArrayList 是一个数组队列,相当于动态数组。它由数组实现,随机访问效率高,随机插入、随机删除效率低。

LinkedList 是一个双向链表。它也可以被当作堆栈、队列或双端队列进行操作。LinkedList随机访问效率低,但随机插入、随机删除效率低。

Vector 是矢量队列,和ArrayList一样,它也是一个动态数组,由数组实现。但是ArrayList是非线程安全的,而Vector是线程安全的。

Stack 是栈,它继承于Vector。它的特性是:先进后出(FILO, First In Last Out)。

第2部分 List使用场景

下面先概括的说明一下各个List的使用场景,后面再分析原因。

如果涉及到“栈”、“队列”、“链表”等操作,应该考虑用List,具体的选择哪个List,根据下面的标准来取舍。

(01) 对于需要快速插入,删除元素,应该使用LinkedList。

(02) 对于需要快速随机访问元素,应该使用ArrayList。

(03)对于“单线程环境” 或者 “多线程环境,但List仅仅只会被单个线程操作”,此时应该使用非同步的类(如ArrayList)。

对于“多线程环境,且List可能同时被多个线程操作”,此时,应该使用同步的类(如Vector)。

通过下面的测试程序,验证上面的(01)和(02)结论。参考代码如下:

1 /**

2 * 对比ArrayList和LinkedList的插入、随机读取效率、删除的效率3 *4 * @ClassName: List_test_15 *@authorXingle6 * @date 2014-5-29 下午5:25:117 */

8 public classList_test_1 {9 private static int COUNT_ = 100000;10

11 private static LinkedList linkedList = new LinkedList();12 private static ArrayList arraylist = new ArrayList();13 private static Vector vector = new Vector();14 private static Stack stack = new Stack();15

16 public static voidmain(String[] args) {17 //插入

18 insertByPosition(stack);19 insertByPosition(linkedList);20 insertByPosition(arraylist);21 insertByPosition(vector);22

23

24 //读取

25 readByPosition(stack);26 readByPosition(linkedList);27 readByPosition(arraylist);28 readByPosition(vector);29

30

31 //删除

32 deleteByPosition(stack);33 deleteByPosition(linkedList);34 deleteByPosition(arraylist);35 deleteByPosition(vector);36

37

38 }39

40 /**

41 * 从list的指定位置删除COUNT个元素,并统计时间42 *43 *@authorxingle44 * @data 2014-5-29 下午5:33:5545 */

46 private static void deleteByPosition(Listlist) {47 long start =getCurrentTime();48 for (int i = 0; i < COUNT_; i++) {49 list.remove(0);50 }51 long end =getCurrentTime();52 long interval = end -start;53 System.out.println(getListName(list) + " : delete " +COUNT_54 + " delete "+COUNT_+" elements from the 1st position use time:" +interval55 + " ms");56

57 }58

59 /**

60 * 根据position,从list中读取元素,并统计时间61 *62 *@paramlist63 *@authorxingle64 * @data 2014-5-29 下午5:32:5865 */

66 private static void readByPosition(Listlist) {67 long start =getCurrentTime();68 for (int i = 0; i < COUNT_; i++) {69 list.get(i);70 }71 long end =getCurrentTime();72 long interval = end -start;73 System.out.println(getListName(list) + " : read " +COUNT_74 + " elements by position use time:" +interval75 + " ms");76

77 }78

79 /**

80 * 向list的指定位置插入COUNT个元素,并统计时间81 *82 *@paramlist83 *@authorxingle84 * @data 2014-5-29 下午5:32:1685 */

86 private static void insertByPosition(Listlist) {87 long start =getCurrentTime();88 for (int i = 0; i < COUNT_; i++) {89 list.add(0, i);90 }91 long end =getCurrentTime();92 long interval = end -start;93 System.out.println(getListName(list) + " : insert " +COUNT_94 + " elements into the 1st position use time:" +interval95 + " ms");96 }97

98 /**

99 * 获取list名称100 *101 *@return

102 *@authorxingle103 * @data 2014-5-29 下午5:38:02104 */

105 private static String getListName(Listlist) {106 if (list instanceofLinkedList)107 return "LinkedList";108 else if (list instanceofArrayList)109 return "ArrayList";110 else if (list instanceofStack)111 return "Stack";112 else if(list instanceofVector)113 return "Vector";114 else

115 return "List";116 }117

118 /**

119 * 获取当前时间120 *121 *@return

122 *@authorxingle123 * @data 2014-5-29 下午5:35:33124 */

125 private static longgetCurrentTime() {126 returnSystem.currentTimeMillis();127 }128

129 }

执行结果:

Stack : insert 100000 elements into the 1st position use time:1724 ms

LinkedList : insert 100000 elements into the 1st position use time:31 ms

ArrayList : insert 100000 elements into the 1st position use time:1724 ms

Vector : insert 100000 elements into the 1st position use time:1651 ms

Stack : read 100000 elements by position use time:9 ms

LinkedList : read 100000 elements by position use time:8969 ms

ArrayList : read 100000 elements by position use time:10 ms

Vector : read 100000 elements by position use time:10 ms

Stack : delete 100000 delete 100000 elements from the 1st position use time:2674 ms

LinkedList : delete 100000 delete 100000 elements from the 1st position use time:23 ms

ArrayList : delete 100000 delete 100000 elements from the 1st position use time:2757 ms

Vector : delete 100000 delete 100000 elements from the 1st position use time:2087 ms

从中,我们可以发现:

插入10万个元素,LinkedList所花时间最短:31ms。

删除10万个元素,LinkedList所花时间最短:23ms。

遍历10万个元素,LinkedList所花时间最长:8969 ms;而ArrayList、Stack和Vector则相差不多,都只用了几秒。

考虑到Vector是支持同步的,而Stack又是继承于Vector的;因此,得出结论:

(01) 对于需要快速插入,删除元素,应该使用LinkedList。

(02) 对于需要快速随机访问元素,应该使用ArrayList。

(03)对于“单线程环境” 或者 “多线程环境,但List仅仅只会被单个线程操作”,此时应该使用非同步的类。

第3部分 LinkedList和ArrayList性能差异分析

下面我们看看为什么LinkedList中插入元素很快,而ArrayList中插入元素很慢!

LinkedList.java中向指定位置插入元素的代码如下:

/*** Inserts the specified element at the specified position in this

* list. Shifts the element currently at that position (if any) and

* any subsequent elements to the right (adds one to their indices).

*

*@paramindex index at which the specified element is to be inserted

*@paramelement element to be inserted

*@throwsIndexOutOfBoundsException {@inheritDoc}*/

public void add(intindex, E element) {

rangeCheckForAdd(index);

ensureCapacityInternal(size+ 1); //Increments modCount!!

System.arraycopy(elementData, index, elementData, index + 1,

size-index);

elementData[index]=element;

size++;

}private void ensureCapacityInternal(intminCapacity) {if (elementData ==EMPTY_ELEMENTDATA) {

minCapacity=Math.max(DEFAULT_CAPACITY, minCapacity);

}

ensureExplicitCapacity(minCapacity);

}private void ensureExplicitCapacity(intminCapacity) {

modCount++;//overflow-conscious code

if (minCapacity - elementData.length > 0)

grow(minCapacity);

}/*** Increases the capacity to ensure that it can hold at least the

* number of elements specified by the minimum capacity argument.

*

*@paramminCapacity the desired minimum capacity*/

private void grow(intminCapacity) {//overflow-conscious code

int oldCapacity =elementData.length;int newCapacity = oldCapacity + (oldCapacity >> 1);if (newCapacity - minCapacity < 0)

newCapacity=minCapacity;if (newCapacity - MAX_ARRAY_SIZE > 0)

newCapacity=hugeCapacity(minCapacity);//minCapacity is usually close to size, so this is a win:

elementData =Arrays.copyOf(elementData, newCapacity);

}private static int hugeCapacity(intminCapacity) {if (minCapacity < 0) //overflow

throw newOutOfMemoryError();return (minCapacity > MAX_ARRAY_SIZE) ?Integer.MAX_VALUE :

MAX_ARRAY_SIZE;

}public static T[] copyOf(U[] original, int newLength, Class extends T[]>newType) {

T[] copy= ((Object)newType == (Object)Object[].class)? (T[]) newObject[newLength]

: (T[]) Array.newInstance(newType.getComponentType(), newLength);

System.arraycopy(original,0, copy, 0,

Math.min(original.length, newLength));returncopy;

}

ensureCapacity(size+1) 的作用是“确认ArrayList的容量,若容量不够,则增加容量。”

真正耗时的操作是 System.arraycopy(elementData, index, elementData, index + 1, size - index);

Sun JDK包的java/lang/System.java中的arraycopy()声明如下:

public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

arraycopy()是个JNI函数,它是在JVM中实现的。sunJDK中看不到源码,不过可以在OpenJDK包中看到的源码。网上有对arraycopy()的分析说明,请参考:实际上,System.arraycopy(elementData, index, elementData, index + 1, size - index); 会移动index之后所有元素即可。这就意味着,ArrayList的add(int index, E element)函数,会引起index之后所有元素的改变!

通过上面的分析,我们就能理解为什么LinkedList中插入元素很快,而ArrayList中插入元素很慢。

“删除元素”与“插入元素”的原理类似,这里就不再过多说明。

接下来,我们看看“为什么LinkedList中随机访问很慢,而ArrayList中随机访问很快”。

先看看LinkedList随机访问的代码

/*** Returns the element at the specified position in this list.

*

*@paramindex index of the element to return

*@returnthe element at the specified position in this list

*@throwsIndexOutOfBoundsException {@inheritDoc}*/

public E get(intindex) {

checkElementIndex(index);returnnode(index).item;

}/*** Returns the (non-null) Node at the specified element index.*/Node node(intindex) {//assert isElementIndex(index);

if (index < (size >> 1)) {

Node x =first;for (int i = 0; i < index; i++)

x=x.next;returnx;

}else{

Node x =last;for (int i = size - 1; i > index; i--)

x=x.prev;returnx;

}

}private static class Node{

E item;

Nodenext;

Nodeprev;

Node(Node prev, E element, Nodenext) {this.item =element;this.next =next;this.prev =prev;

}

}

从中,可以看出:通过get(int index)获取LinkedList第index个元素时。先是在双向链表中找到要index位置的元素;找到之后再返回。

双向链表查找index位置的节点时,有一个加速动作:若index < 双向链表长度的1/2,则从前向后查找; 否则,从后向前查找。

下面看看ArrayList随机访问的代码

/*** Returns the element at the specified position in this list.

*

*@paramindex index of the element to return

*@returnthe element at the specified position in this list

*@throwsIndexOutOfBoundsException {@inheritDoc}*/

public E get(intindex) {

rangeCheck(index);returnelementData(index);

}//Positional Access Operations

@SuppressWarnings("unchecked")

E elementData(intindex) {return(E) elementData[index];

}

从中,可以看出:通过get(int index)获取ArrayList第index个元素时。直接返回数组中index位置的元素,而不需要像LinkedList一样进行查找。

第4部分 Vector和ArrayList比较

相同之处

1 它们都是List

它们都继承于AbstractList,并且实现List接口。

ArrayList和Vector的类定义如下:

//ArrayList的定义

public class ArrayList extends AbstractList

implements List, RandomAccess, Cloneable, java.io.Serializable//Vector的定义

public class Vector

extends AbstractList

implements List, RandomAccess, Cloneable, java.io.Serializable

2 它们都实现了RandomAccess和Cloneable接口

实现RandomAccess接口,意味着它们都支持快速随机访问;

实现Cloneable接口,意味着它们能克隆自己。

3 它们都是通过数组实现的,本质上都是动态数组

ArrayList.java中定义数组elementData用于保存元素

/*** 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 == EMPTY_ELEMENTDATA will be expanded to

* DEFAULT_CAPACITY when the first element is added.*/

private transient Object[] elementData;

Vector.java中也定义了数组elementData用于保存元素

/*** The array buffer into which the components of the vector are

* stored. The capacity of the vector is the length of this array buffer,

* and is at least large enough to contain all the vector's elements.

*

*

Any array elements following the last element in the Vector are null.

*

*@serial

*/

protected Object[] elementData;

4 它们的默认数组容量是10

若创建ArrayList或Vector时,没指定容量大小;则使用默认容量大小10。

ArrayList的默认构造函数如下:

/*** Constructs an empty list with an initial capacity of ten.*/

publicArrayList() {super();this.elementData =EMPTY_ELEMENTDATA;

}/*** Default initial capacity.*/

private static final int DEFAULT_CAPACITY = 10;

Vector的默认构造函数如下:

/*** Constructs an empty vector so that its internal data array

* has size {@code10} and its standard capacity increment is

* zero.*/

publicVector() {this(10);

}

5 它们都支持Iterator和listIterator遍历

它们都继承于AbstractList,而AbstractList中分别实现了 “iterator()接口返回Iterator迭代器” 和 “listIterator()返回ListIterator迭代器”。

不同之处

1 线程安全性不一样

ArrayList是非线程安全;

而Vector是线程安全的,它的函数都是synchronized的,即都是支持同步的。

ArrayList适用于单线程,Vector适用于多线程。

2 对序列化支持不同

ArrayList支持序列化,而Vector不支持;即ArrayList有实现java.io.Serializable接口,而Vector没有实现该接口。

3 构造函数个数不同

ArrayList有3个构造函数,而Vector有4个构造函数。Vector除了包括和ArrayList类似的3个构造函数之外,另外的一个构造函数可以指定容量增加系数。

ArrayList的构造函数如下:

/*** capacity是ArrayList的默认容量大小。当由于增加数据导致容量不足时,容量会添加上一次容量大小的一半

* Constructs an empty list with the specified initial capacity.

*

*@paraminitialCapacity the initial capacity of the list

*@throwsIllegalArgumentException if the specified initial capacity

* is negative*/

public ArrayList(intinitialCapacity) {super();if (initialCapacity < 0)throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);this.elementData = newObject[initialCapacity];

}/*** 默认构造函数

* Constructs an empty list with an initial capacity of ten.*/

publicArrayList() {super();this.elementData =EMPTY_ELEMENTDATA;

}/*** Constructs a list containing the elements of the specified

* collection, in the order they are returned by the collection's

* iterator.

*

*@paramc the collection whose elements are to be placed into this list

*@throwsNullPointerException if the specified collection is null*/

public ArrayList(Collection extends E>c) {

elementData=c.toArray();

size=elementData.length;//c.toArray might (incorrectly) not return Object[] (see 6260652)

if (elementData.getClass() != Object[].class)

elementData= Arrays.copyOf(elementData, size, Object[].class);

}

Vector的构造函数如下:

/*** Constructs an empty vector with the specified initial capacity and

* capacity increment.

*

*@paraminitialCapacity the initial capacity of the vector

*@paramcapacityIncrement the amount by which the capacity is

* increased when the vector overflows

*@throwsIllegalArgumentException if the specified initial capacity

* is negative*/

public Vector(int initialCapacity, intcapacityIncrement) {super();if (initialCapacity < 0)throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);this.elementData = newObject[initialCapacity];this.capacityIncrement =capacityIncrement;

}/*** Constructs an empty vector with the specified initial capacity and

* with its capacity increment equal to zero.

*

*@paraminitialCapacity the initial capacity of the vector

*@throwsIllegalArgumentException if the specified initial capacity

* is negative*/

public Vector(intinitialCapacity) {this(initialCapacity, 0);

}/*** Constructs an empty vector so that its internal data array

* has size {@code10} and its standard capacity increment is

* zero.*/

publicVector() {this(10);

}/*** Constructs a vector containing the elements of the specified

* collection, in the order they are returned by the collection's

* iterator.

*

*@paramc the collection whose elements are to be placed into this

* vector

*@throwsNullPointerException if the specified collection is null

*@since1.2*/

public Vector(Collection extends E>c) {

elementData=c.toArray();

elementCount=elementData.length;//c.toArray might (incorrectly) not return Object[] (see 6260652)

if (elementData.getClass() != Object[].class)

elementData= Arrays.copyOf(elementData, elementCount, Object[].class);

}

4 容量增加方式不同

逐个添加元素时,若ArrayList容量不足时,新的容量扩大1.5倍。

ArrayList中容量增长的主要函数如下:

/*** Increases the capacity to ensure that it can hold at least the

* number of elements specified by the minimum capacity argument.

*

*@paramminCapacity the desired minimum capacity*/

private void grow(intminCapacity) {//overflow-conscious code

int oldCapacity =elementData.length;int newCapacity = oldCapacity + (oldCapacity >> 1);//对容量扩大1.5倍

if (newCapacity - minCapacity < 0)

newCapacity=minCapacity;if (newCapacity - MAX_ARRAY_SIZE > 0)

newCapacity=hugeCapacity(minCapacity);//minCapacity is usually close to size, so this is a win:

elementData =Arrays.copyOf(elementData, newCapacity);

}

Vector中容量增长的主要函数如下:

在进行动态扩容时,Vector的新容量大小为原有容量加上capacityIncrement,如果这个数不大于0,则扩容为原始容量的2倍。

private void grow(intminCapacity) {//overflow-conscious code

int oldCapacity =elementData.length;int newCapacity = oldCapacity + ((capacityIncrement > 0) ?capacityIncrement : oldCapacity);if (newCapacity - minCapacity < 0)

newCapacity=minCapacity;if (newCapacity - MAX_ARRAY_SIZE > 0)

newCapacity=hugeCapacity(minCapacity);

elementData=Arrays.copyOf(elementData, newCapacity);

}

5 对Enumeration的支持不同。Vector支持通过Enumeration去遍历,而List不支持

Vector中实现Enumeration的代码如下:

/*** Returns an enumeration of the components of this vector. The

* returned {@codeEnumeration} object will generate all items in

* this vector. The first item generated is the item at index {@code0},

* then the item at index {@code1}, and so on.

*

*@returnan enumeration of the components of this vector

*@seeIterator*/

public Enumerationelements() {return new Enumeration() {int count = 0;public booleanhasMoreElements() {return count

}publicE nextElement() {synchronized (Vector.this) {if (count

}

}throw new NoSuchElementException("Vector Enumeration");

}

};

}

LIST函数JAVA特点_Java 集合系列 07 List总结(LinkedList, ArrayList等使用场景和性能分析)...相关推荐

  1. java集合系列_Java集合系列01-Java集合概述

    1.Java集合基本概念 在编程中,常常需要集中存放多个数据.从传统意义上讲,数组是我们的一个很好的选择,前提是我们事先已经明确知道我们将要保存的对象的数量,因为数组长度在初始化时指定,意味着只能保存 ...

  2. java中集合怎么定义_Java集合系列(一):集合的定义及分类

    1. 集合的定义 什么是集合呢? 定义:集合是一个存放对象的引用的容器. 在Java中,集合位于java.util包下. 2. 集合和数组的区别(面试常问) 提到容器,就会想起数组,那么集合和数组的区 ...

  3. java队列_java集合入门和深入学习(详解),看这篇就差不多了

    一.集合入门总结 集合框架: Java中的集合框架大类可分为Collection和Map:两者的区别: 1.Collection是单列集合:Map是双列集合 2.Collection中只有Set系列要 ...

  4. list清空的函数java,6-1 jmu-Java-05集合-List中指定元素的删除 (20分)

    编写如下两个函数java /以空格(单个或多个)为分隔符,将line中的元素抽取出来,放入一个List/ public static List convertStringToList(String l ...

  5. Java ArrayList、LinkedList和Vector的使用及性能分析

    第1部分 List概括 List 是一个接口,它继承于Collection的接口.它代表着有序的队列. AbstractList 是一个抽象类,它继承于AbstractCollection.Abstr ...

  6. 代码函数从零开始学习OpenCL开发(二)一个最简单的示例与简单性能分析

    在本文中,我们要主介绍代码函数的内容,自我感觉有个不错的建议和大家分享下 迎欢存眷 转载请注明 http://blog.csdn.net/leonwei/article/details/8893796 ...

  7. java set复制_Java 集合系列之三:Set基本操作

    1. Java Set 1. Java Set 重要观点 Java Set接口是Java Collections Framework的成员. Set不允许出现重复元素-----------无重复 Se ...

  8. java 取栈顶元素_java集合系列(7)Stack

    这篇文章开始介绍Stack.从名字看他就是一个stack,因此具有数据结构中栈的一般特性(后进先出),平时用起来相对较多一点,但是也是非常简单.这篇文章我们将从源码的角度来分析一下Stack. OK, ...

  9. java list 博客园_Java集合系列(一)List集合

    List的几种实现的区别与联系 List主要有ArrayList.LinkedList与Vector几种实现. ArrayList底层数据结构是数组, 增删慢.查询快; 线程不安全, 效率高; 不可以 ...

最新文章

  1. Android项目中创建编译期的注解
  2. 第二章、IP协议详解
  3. aspose word 获取标题_Word干货|多级标题的自动编号怎么添加?
  4. 行云管家堡垒机 导入腾讯云主机
  5. Android 8.0(30)--- 应用快捷方式(ShortcutManager)的使用
  6. Java中static变量作用和用法详解
  7. 深入理解js构造函数
  8. ]MySQL操作命令语句实例
  9. linux终端使用python的matplotlib模块画图出现“could not open display”问题解决
  10. 外汇交易系统(自动化/程序化/量化/EA)
  11. 【vivado使用】第2篇:超实用的vivado使用教程
  12. 如何从云桌面(远程桌面)拷贝文件
  13. QT对字符串简单加、解密
  14. 注册微信小程序并开通微信支付流程
  15. 闲聊Robots协议
  16. RPM包安装的数据库进行版本升级
  17. Psins代码解析之线性误差模型精度验证(test_SINS_error_model_verify.m)
  18. php实现团购功能,PHP 对接美团大众点评团购券(门票)的开发步骤
  19. 慕课Java第三季学习及笔记整理
  20. oracle 6i字体变大,将iTunes 12字体大小更改为更大或更小 | MOS86

热门文章

  1. 有没有什么方法快速能找到导致软件崩溃的进程_手机软件闪退闪得怀疑人生?看我专治闪退二十年!...
  2. python中or是什么意思-Python 中 (,|)和(and,or)之间的区别
  3. ci 页面php代码,Cicool v3.1.0 - PHP页面、表单、API、CRUD生成器
  4. LeetCode-70 爬楼梯
  5. crontrigger java,CronTrigger
  6. map语法获取index_python获取慧聪企业信息
  7. QT每日一练day28:QT中的2D绘图知识概念——讲义截图
  8. linux怎么添加ubuntu源,细说 Ubuntu Linux 软件源
  9. linux内核实时调度,基于Linux内核的实时调度机制的研究和实现
  10. ddr布线 pads_DDR SDRAM布线规则