RSS
热门关键字:  java  Ajax  JSP  Struts  JSF
当前位置 : 首页>Java>列表

基于JDK5.0一些collection类的使用总结

来源: 作者: 时间:2008-03-11 点击:

查找一个对象:
i = Collections.binarySearch(c, element);
i = Collections.binarySearch(c, element, comparator);
但是这些list必须是已经排好序了。而且要注意的是这个算法需要随机访问collection,如果不支持随机访问那么这个算法的效率可能会很低。

几种常用Collection:
ArrayList
An indexed sequence that grows and shrinks dynamically
可以随机访问,但是如果要从中间删除一个对象会影响效率,因为有些未删除的对象要相应的调整位置。非线程安全,但效率会比Vector要高,如果在单线程下,选它而不是Vector。

LinkedList
An ordered sequence that allows efficient insertions and removal at any location
只能按顺序访问,添加删除很方便。虽然提供了get(n)方法,但实际上还是顺序访问的,如果发现在LinkedList里面使用了这个方法,要考虑这个List类型是否选的合适

HashSet
An unordered collection that rejects duplicates
以hashcode为索引,适用于不知道所存对象位置而想寻找某个对象的情况。不可重复

TreeSet
A sorted set
与HashSet类似,但是所存对象是排了序的

LinkedHashSet
A set that remembers the order in which elements were inserted


PriorityQueue
A collection that allows efficient removal of the smallest element
加入Queue的时候会给与一个优先级,从queue中取出的时候先取出优先级最低的

HashMap
A data structure that stores key/value associations
存储key/value对,非线程安全,与HashTable相比效率要高些

treeMap
A map in which the keys are sorted
排序的HashMap

LinkedHashMap
A map that remembers the order in which entries were added  

共2页: 上一页 [1] 2 下一页
上一篇:业务逻辑和数据库访问决策
下一篇:没有了
最新评论共有 1 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
Google Adsense
相关文章