java容器遍历_Java集合容器总结

Java集合容器总结

一、按数据结构主要有以下几类:内置、list、set、map;

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-valuepairs。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、线程安全

使用锁:完全不支持并发:

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

LinkedBlockingQueue,PriorityBlockingQueue,SynchronousQueue

set容器:CopyOnWriteArraySet

map容器:Hashtable

使用锁:部分支持并发:

list容器:无

set容器:无

map容器:ConcurrentHashMap

使用非阻塞算法:

list容器:ConcurrentLinkedQueue

set容器:无

map容器:无

2、非线程安全:

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

set容器:HashSet,TreeSet,LinkedHashSet,EnumSet

map容器:HashMap,TreeMap,LinkedHashMap,WeakHashMap,IdentityHashMap,EnumMap

四、按遍历安全主要有以下几类:

1、遍历安全:

可并发遍历:

list容器:CopyOnWriteArrayList,ConcurrentLinkedQueue

set容器:CopyOnWriteArraySet,EnumSet,EnumMap

map容器:无

不可并发遍历:

list容器:Vetor,Stack,Hashtable,ArrayBlockingQueue,    LinkedBlockingQueue,PriorityBlockingQueue,SynchronousQueue

set容器:无

map容器:Hashtable,concurrentHashMap

注意1:concurrentHashMap迭代器它们不会抛出ConcurrentModificationException。不过,迭代器被设计成每次仅由一个线程使用。

2、遍历不安全:

会抛异常ConcurrentModificationException:

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

set容器:HashSet,TreeSet,TreeSet,LinkedHashSet

map容器:HashMap,TreeMap,LinkedHashMap,WeakHashMap,IdentityHashMap

注意1:返回的迭代器是弱一致 的:它们不会抛出 ConcurrentModificationException,

也不一定显示在迭代进行时发生的任何映射修改的效果的容器有:

EnumSet,EnumMap

五、按遍历是否有序性分类

存储数据有序:

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.get(i);

的运行速度要快于以下循环:

for (Iterator i=list.iterator(); i.hasNext(); )

i.next();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值