Java集合容器总结

2010-08-12 12:36:44|  分类: JAVA集合容器|举报|字号 订阅

 
 
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容器:V etor,Stack,CopyOnWriteArrayList,ArrayBlockingQueue,
  LinkedBlockingQueue,PriorityBlockingQueue,SynchronousQueue

   set容器CopyOnWriteArraySet
   map容器Hashtable
   部分支持并发
  l ist容器:无
   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

注意1EnumSet , 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

猜你喜欢

转载自huaonline.iteye.com/blog/2017313