java集合08--List总结

转载地址:http://blog.csdn.net/wangxiaotongfan/article/details/51332950

概要

前面,我们学完了List的全部内容,详细内容请看前面的几篇博客,

现在,我们再回头看看总结一下List。内容包括:
第1部分 List概括
第2部分 List使用场景
3部分 LinkedList和ArrayList性能差异分析
第4部分 Vector和ArrayList比较

第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)结论。参考代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import java.util.*;  
  2. import java.lang.Class;  
  3.   
  4. /* 
  5.  * @desc 对比ArrayList和LinkedList的插入、随机读取效率、删除的效率 
  6.  * 
  7.  * @author skywang 
  8.  */  
  9. public class ListCompareTest {  
  10.   
  11.     private static final int COUNT = 100000;  
  12.   
  13.     private static LinkedList linkedList = new LinkedList();  
  14.     private static ArrayList arrayList = new ArrayList();  
  15.     private static Vector vector = new Vector();  
  16.     private static Stack stack = new Stack();  
  17.   
  18.     public static void main(String[] args) {  
  19.         // 换行符  
  20.         System.out.println();  
  21.         // 插入  
  22.         insertByPosition(stack) ;  
  23.         insertByPosition(vector) ;  
  24.         insertByPosition(linkedList) ;  
  25.         insertByPosition(arrayList) ;  
  26.   
  27.         // 换行符  
  28.         System.out.println();  
  29.         // 随机读取  
  30.         readByPosition(stack);  
  31.         readByPosition(vector);  
  32.         readByPosition(linkedList);  
  33.         readByPosition(arrayList);  
  34.   
  35.         // 换行符  
  36.         System.out.println();  
  37.         // 删除   
  38.         deleteByPosition(stack);  
  39.         deleteByPosition(vector);  
  40.         deleteByPosition(linkedList);  
  41.         deleteByPosition(arrayList);  
  42.     }  
  43.   
  44.     // 获取list的名称  
  45.     private static String getListName(List list) {  
  46.         if (list instanceof LinkedList) {  
  47.             return "LinkedList";  
  48.         } else if (list instanceof ArrayList) {  
  49.             return "ArrayList";  
  50.         } else if (list instanceof Stack) {  
  51.             return "Stack";  
  52.         } else if (list instanceof Vector) {  
  53.             return "Vector";  
  54.         } else {  
  55.             return "List";  
  56.         }  
  57.     }  
  58.   
  59.     // 向list的指定位置插入COUNT个元素,并统计时间  
  60.     private static void insertByPosition(List list) {  
  61.         long startTime = System.currentTimeMillis();  
  62.   
  63.         // 向list的位置0插入COUNT个数  
  64.         for (int i=0; i<COUNT; i++)  
  65.             list.add(0, i);  
  66.   
  67.         long endTime = System.currentTimeMillis();  
  68.         long interval = endTime - startTime;  
  69.         System.out.println(getListName(list) + " : insert "+COUNT+" elements into the 1st position use time:" + interval+" ms");  
  70.     }  
  71.   
  72.     // 从list的指定位置删除COUNT个元素,并统计时间  
  73.     private static void deleteByPosition(List list) {  
  74.         long startTime = System.currentTimeMillis();  
  75.   
  76.         // 删除list第一个位置元素  
  77.         for (int i=0; i<COUNT; i++)  
  78.             list.remove(0);  
  79.   
  80.         long endTime = System.currentTimeMillis();  
  81.         long interval = endTime - startTime;  
  82.         System.out.println(getListName(list) + " : delete "+COUNT+" elements from the 1st position use time:" + interval+" ms");  
  83.     }  
  84.   
  85.     // 根据position,不断从list中读取元素,并统计时间  
  86.     private static void readByPosition(List list) {  
  87.         long startTime = System.currentTimeMillis();  
  88.   
  89.         // 读取list元素  
  90.         for (int i=0; i<COUNT; i++)  
  91.             list.get(i);  
  92.   
  93.         long endTime = System.currentTimeMillis();  
  94.         long interval = endTime - startTime;  
  95.         System.out.println(getListName(list) + " : read "+COUNT+" elements by position use time:" + interval+" ms");  
  96.     }  
  97. }  

运行结果如下

  1. Stack : insert 100000 elements into the 1st position use time:1640 ms  
  2. Vector : insert 100000 elements into the 1st position use time:1607 ms  
  3. LinkedList : insert 100000 elements into the 1st position use time:29 ms  
  4. ArrayList : insert 100000 elements into the 1st position use time:1617 ms  
  5.   
  6. Stack : read 100000 elements by position use time:9 ms  
  7. Vector : read 100000 elements by position use time:6 ms  
  8. LinkedList : read 100000 elements by position use time:10809 ms  
  9. ArrayList : read 100000 elements by position use time:5 ms  
  10.   
  11. Stack : delete 100000 elements from the 1st position use time:1916 ms  
  12. Vector : delete 100000 elements from the 1st position use time:1910 ms  
  13. LinkedList : delete 100000 elements from the 1st position use time:15 ms  
  14. ArrayList : delete 100000 elements from the 1st position use time:1909 ms  

从中,我们可以发现
插入10万个元素,LinkedList所花时间最短:29ms
删除10万个元素,LinkedList所花时间最短:15ms
遍历10万个元素,LinkedList所花时间最长:10809 ms;而ArrayList、Stack和Vector则相差不多,都只用了几秒。

考虑到Vector是支持同步的,而Stack又是继承于Vector的;因此,得出结论:
(01) 对于需要快速插入,删除元素,应该使用LinkedList。
(02) 对于需要快速随机访问元素,应该使用ArrayList。
(03) 对于“单线程环境” 或者 “多线程环境,但List仅仅只会被单个线程操作”,此时应该使用非同步的类。

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

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

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

  1. // 在index前添加节点,且节点的值为element  
  2. public void add(int index, E element) {  
  3.     addBefore(element, (index==size ? header : entry(index)));  
  4. }  
  5.   
  6. // 获取双向链表中指定位置的节点  
  7. private Entry<E> entry(int index) {  
  8.     if (index < 0 || index >= size)  
  9.         throw new IndexOutOfBoundsException("Index: "+index+  
  10.                                             ", Size: "+size);  
  11.     Entry<E> e = header;  
  12.     // 获取index处的节点。  
  13.     // 若index < 双向链表长度的1/2,则从前向后查找;  
  14.     // 否则,从后向前查找。  
  15.     if (index < (size >> 1)) {  
  16.         for (int i = 0; i <= index; i++)  
  17.             e = e.next;  
  18.     } else {  
  19.         for (int i = size; i > index; i--)  
  20.             e = e.previous;  
  21.     }  
  22.     return e;  
  23. }  
  24.   
  25. // 将节点(节点数据是e)添加到entry节点之前。  
  26. private Entry<E> addBefore(E e, Entry<E> entry) {  
  27.     // 新建节点newEntry,将newEntry插入到节点e之前;并且设置newEntry的数据是e  
  28.     Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);  
  29.     // 插入newEntry到链表中  
  30.     newEntry.previous.next = newEntry;  
  31.     newEntry.next.previous = newEntry;  
  32.     size++;  
  33.     modCount++;  
  34.     return newEntry;  
  35. }  

从中,我们可以看出:通过add(int index, E element)向LinkedList插入元素时。先是在双向链表中找到要插入节点的位置index;找到之后,再插入一个新节点
双向链表查找index位置的节点时,有一个加速动作若index < 双向链表长度的1/2,则从前向后查找; 否则,从后向前查找

接着,我们看看ArrayList.java中向指定位置插入元素的代码。如下:

  1. // 将e添加到ArrayList的指定位置  
  2. public void add(int index, E element) {  
  3.     if (index > size || index < 0)  
  4.         throw new IndexOutOfBoundsException(  
  5.         "Index: "+index+", Size: "+size);  
  6.   
  7.     ensureCapacity(size+1);  // Increments modCount!!  
  8.     System.arraycopy(elementData, index, elementData, index + 1,  
  9.          size - index);  
  10.     elementData[index] = element;  
  11.     size++;  
  12. }  

ensureCapacity(size+1) 的作用是“确认ArrayList的容量,若容量不够,则增加容量。
真正耗时的操作是 System.arraycopy(elementData, index, elementData, index + 1, size - index);

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

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

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

通过上面的分析,我们就能理解为什么LinkedList中插入元素很快,而ArrayList中插入元素很慢。
“删除元素”与“插入元素”的原理类似,这里就不再过多说明。

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

先看看LinkedList随机访问的代码

  1. // 返回LinkedList指定位置的元素  
  2. public E get(int index) {  
  3.     return entry(index).element;  
  4. }  
  5.   
  6. // 获取双向链表中指定位置的节点  
  7. private Entry<E> entry(int index) {  
  8.     if (index < 0 || index >= size)  
  9.         throw new IndexOutOfBoundsException("Index: "+index+  
  10.                                             ", Size: "+size);  
  11.     Entry<E> e = header;  
  12.     // 获取index处的节点。  
  13.     // 若index < 双向链表长度的1/2,则从前先后查找;  
  14.     // 否则,从后向前查找。  
  15.     if (index < (size >> 1)) {  
  16.         for (int i = 0; i <= index; i++)  
  17.             e = e.next;  
  18.     } else {  
  19.         for (int i = size; i > index; i--)  
  20.             e = e.previous;  
  21.     }  
  22.     return e;  
  23. }  

从中,我们可以看出:通过get(int index)获取LinkedList第index个元素时先是在双向链表中找到要index位置的元素;找到之后再返回。
双向链表查找index位置的节点时,有一个加速动作若index < 双向链表长度的1/2,则从前向后查找; 否则,从后向前查找。

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

  1. // 获取index位置的元素值  
  2. public E get(int index) {  
  3.     RangeCheck(index);  
  4.   
  5.     return (E) elementData[index];  
  6. }  
  7.   
  8. private void RangeCheck(int index) {  
  9.     if (index >= size)  
  10.         throw new IndexOutOfBoundsException(  
  11.         "Index: "+index+", Size: "+size);  
  12. }  

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

第4部分 Vector和ArrayList比较

相同之处

1 它们都是List

它们都继承于AbstractList,并且实现List接口。
ArrayList和Vector的类定义如下:

  1. // ArrayList的定义  
  2. public class ArrayList<E> extends AbstractList<E>  
  3.         implements List<E>, RandomAccess, Cloneable, java.io.Serializable  
  4.   
  5. // Vector的定义  
  6. public class Vector<E> extends AbstractList<E>  
  7.     implements List<E>, RandomAccess, Cloneable, java.io.Serializable {}  

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

   实现RandomAccess接口,意味着它们都支持快速随机访问;
   实现Cloneable接口,意味着它们能克隆自己。

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

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

  1. // 保存ArrayList中数据的数组  
  2. private transient Object[] elementData;  

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

  1. // 保存Vector中数据的数组  
  2. protected Object[] elementData;  

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

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

ArrayList的默认构造函数如下:

  1. // ArrayList构造函数。默认容量是10。  
  2. public ArrayList() {  
  3.     this(10);  
  4. }  

Vector的默认构造函数如下:

  1. // Vector构造函数。默认容量是10。  
  2. public Vector() {  
  3.     this(10);  
  4. }   

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的构造函数如下

  1. // 默认构造函数  
  2. ArrayList()  
  3.   
  4. // capacity是ArrayList的默认容量大小。当由于增加数据导致容量不足时,容量会添加上一次容量大小的一半。  
  5. ArrayList(int capacity)  
  6.   
  7. // 创建一个包含collection的ArrayList  
  8. ArrayList(Collection<? extends E> collection)  

Vector的构造函数如下

  1. // 默认构造函数  
  2. Vector()  
  3.   
  4. // capacity是Vector的默认容量大小。当由于增加数据导致容量增加时,每次容量会增加一倍。  
  5. Vector(int capacity)  
  6.   
  7. // 创建一个包含collection的Vector  
  8. Vector(Collection<? extends E> collection)  
  9.   
  10. // capacity是Vector的默认容量大小,capacityIncrement是每次Vector容量增加时的增量值。  
  11. Vector(int capacity, int capacityIncrement)  

4 容量增加方式不同

   逐个添加元素时,若ArrayList容量不足时,“新的容量”=“(原始容量x3)/2 + 1”。
   而Vector的容量增长与“增长系数有关”,若指定了“增长系数”,且“增长系数有效(即,大于0)”;那么,每次容量不足时,“新的容量”=“原始容量+增长系数”。若增长系数无效(即,小于/等于0),则“新的容量”=“原始容量 x 2”。

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

  1. public void ensureCapacity(int minCapacity) {  
  2.     // 将“修改统计数”+1  
  3.     modCount++;  
  4.     int oldCapacity = elementData.length;  
  5.     // 若当前容量不足以容纳当前的元素个数,设置 新的容量=“(原始容量x3)/2 + 1”  
  6.     if (minCapacity > oldCapacity) {  
  7.         Object oldData[] = elementData;  
  8.         int newCapacity = (oldCapacity * 3)/2 + 1;  
  9.         if (newCapacity < minCapacity)  
  10.             newCapacity = minCapacity;  
  11.         elementData = Arrays.copyOf(elementData, newCapacity);  
  12.     }  
  13. }  

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

  1. private void ensureCapacityHelper(int minCapacity) {  
  2.     int oldCapacity = elementData.length;  
  3.     // 当Vector的容量不足以容纳当前的全部元素,增加容量大小。  
  4.     // 若 容量增量系数>0(即capacityIncrement>0),则将容量增大当capacityIncrement  
  5.     // 否则,将容量增大一倍。  
  6.     if (minCapacity > oldCapacity) {  
  7.         Object[] oldData = elementData;  
  8.         int newCapacity = (capacityIncrement > 0) ?  
  9.             (oldCapacity + capacityIncrement) : (oldCapacity * 2);  
  10.         if (newCapacity < minCapacity) {  
  11.             newCapacity = minCapacity;  
  12.         }  
  13.         elementData = Arrays.copyOf(elementData, newCapacity);  
  14.     }  
  15. }  

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

Vector中实现Enumeration的代码如下:

  1. public Enumeration<E> elements() {  
  2.     // 通过匿名类实现Enumeration  
  3.     return new Enumeration<E>() {  
  4.         int count = 0;  
  5.   
  6.         // 是否存在下一个元素  
  7.         public boolean hasMoreElements() {  
  8.             return count < elementCount;  
  9.         }  
  10.   
  11.         // 获取下一个元素  
  12.         public E nextElement() {  
  13.             synchronized (Vector.this) {  
  14.                 if (count < elementCount) {  
  15.                     return (E)elementData[count++];  
  16.                 }  
  17.             }  
  18.             throw new NoSuchElementException("Vector Enumeration");  
  19.         }  
  20.     };  
  21. }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章