Java集合容器總結

來源:互聯網
上載者:User

Java集合容器總結。
按資料結構主要有以下幾類:

 1,內建容器:數組
 2,list容器:Vetor,Stack,ArrayList,LinkedList,

 CopyOnWriteArrayList(1.5),AttributeList(1.5),RoleList(1.5),RoleUnresolvedList(1.5),

 ConcurrentLinkedQueue(1.5),ArrayBlockingQueue(1.5),LinkedBlockingQueue(1.5),

 PriorityQueue(1.5),PriorityBlockingQueue(1.5),SynchronousQueue(1.5)

 3,set容器:HashSet(1.2),LinkedHashSet(1.4),TreeSet(1.2),

 CopyOnWriteArraySet(1.5),EnumSet(1.5),JobStateReasons。
 4,map容器:Hashtable,HashMap(1.2),TreeMap(1.2),LinkedHashMap(1.4),WeakHashMap(1.2),

 IdentityHashMap(1.4),ConcurrentMap(1.5),concurrentHashMap(1.5)。
Set 介面繼承 Collection,但不允許重複,使用自己內部的一個排列機制。
List 介面繼承 Collection,允許重複,以元素安插的次序來放置元素,不會重新排列。
Map介面是一組成對的鍵-值對象,即所持有的是key-value pairs。Map中不能有重複的key。擁有自己的內部排列機制。
按新舊主要有以下幾類:

Java1.2前的容器:Vector,Stack,Hashtable。

Java1.2的容器:HashSet,TreeSet,HashMap,TreeMap,WeakHashMap

Java1.4的容器:LinkedHashSet,LinkedHashMap,IdentityHashMap,ConcurrentMap,concurrentHashMap

java1.5新增:CopyOnWriteArrayList,AttributeList,RoleList,RoleUnresolvedList,

 ConcurrentLinkedQueue,ArrayBlockingQueue,LinkedBlockingQueue,PriorityBlockingQueue

 ArrayBlockingQueue,CopyOnWriteArraySet,EnumSet,

未知:JobStateReasons
按安全執行緒主要有以下幾類:
安全執行緒

一,使用鎖:

  1.1、完全不支援並發:

  list容器:Vetor,Stack,CopyOnWriteArrayList,ArrayBlockingQueue,

  LinkedBlockingQueue,PriorityBlockingQueue,SynchronousQueue

  set容器:CopyOnWriteArraySet

  map容器:Hashtable

  部分支援並發:

  list容器:無

  set容器:無

  map容器:concurrentHashMap

2.2、 使用非阻塞演算法:

 list容器:ConcurrentLinkedQueue

 set容器:無

 map容器:無

二,非安全執行緒:

 list容器:ArrayList,LinkedList,AttributeList,RoleList,RoleUnresolvedList,PriorityQueue

 set容器:HashSet,TreeSet,LinkedHashSet,EnumSet
 map容器:HashMap,TreeMap,LinkedHashMap,WeakHashMap,IdentityHashMap,EnumMap
按遍曆安全主要有以下幾類:

一,遍曆安全:

 可並發遍曆:

   list容器:CopyOnWriteArrayList,ConcurrentLinkedQueue

   set容器:CopyOnWriteArraySet,EnumSet,EnumMap

   map容器:無

 不可並發遍曆:

   list容器:Vetor,Stack,Hashtable,ArrayBlockingQueue,

   LinkedBlockingQueue,PriorityBlockingQueue,SynchronousQueue

   set容器:無

   map容器:Hashtable,concurrentHashMap

注意1:concurrentHashMap迭代器它們不會拋出ConcurrentModificationException。不過,迭代器被設計成每次僅由一個線程使用。 

二,遍曆不安全:
會拋異常ConcurrentModificationException:
list容器:ArrayList,LinkedList,AttributeList,RoleList,RoleUnresolvedList
set容器:HashSet,TreeSet,TreeSet,LinkedHashSet
map容器:HashMap,TreeMap,LinkedHashMap,WeakHashMap,IdentityHashMap


注意1:EnumSet,和EnumMap返回的迭代器是弱一致 的:它們不會拋出 ConcurrentModificationException,   也不一定顯示在迭代進行時發生的任何映射修改。
按是否有序性分類
儲存資料簡單有序:
 list容器: ConcurrentLinkedQueue(1.5),ArrayBlockingQueue(1.5),LinkedBlockingQueue(1.5),

 SynchronousQueue(1.5)

 set容器:TreeSet(1.2).(他們實現了set介面),

 CopyOnWriteArraySet(1.5),EnumSet(1.5),JobStateReasons。

 map容器:TreeMap(1.2),LinkedHashMap(1.4) 。
儲存資料有特定序:

 list容器:Stack,Vetor,ArrayList,LinkedList,CopyOnWriteArrayList(1.5),AttributeList(1.5),RoleList(1.5),RoleUnresolvedList(1.5)
 set容器:無

 map容器:無
遍曆無序但移除有序:

 list容器:PriorityQueue(1.5),PriorityBlockingQueue(1.5)

 set容器:無

 map容器:無
無論如何都無序:
 list容器:無

 set容器:HashSet(1.2),LinkedHashSet(1.4)
 map容器:Hashtable,HashMap(1.2),WeakHashMap(1.2),IdentityHashMap(1.4),

 ConcurrentMap(1.5),concurrentHashMap(1.5)
可以按自然順序(參見 Comparable)或比較子進行排序的有:
list容器:PriorityQueue(1.5),PriorityBlockingQueue
set容器:TreeSet(1.2)
map容器:TreeMap(1.2)
實現了RandomAccess介面的有:
 ArrayList, AttributeList, CopyOnWriteArrayList, RoleList, RoleUnresolvedList, Stack, Vector

RandomAccess介面是List 實現所使用的標記介面,用來表明其支援快速(通常是固定時間)隨機訪問。此介面的主要目的是允許一般的演算法更改其行為,從而在將其應用到隨機或連續訪問列表時能提供良好的效能。
在對List特別的遍曆演算法中,要盡量來判斷是屬於 RandomAccess(如ArrayList)還是SequenceAccess(如LinkedList),
因為適合RandomAccess List的遍曆演算法,用在SequenceAccess List上就差別很大,
即對於實現了RandomAccess介面的類執行個體而言,此迴圈
 for (int i=0, i<list.size(); i++)

   list.get(i);

的運行速度要快於以下迴圈:
     for (Iterator i=list.iterator(); i.hasNext(); )

    i.next();

關於RandomAccess介面更多詳細可以參考《 隨機訪問RandomAccess
容量不能自動擴充的有:
ArrayBlockingQueue

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.