Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 基于JDK5.0的一些collection类的使用总结
wangjiong





发贴: 17
积分: 17
于 2005-11-09 13:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在5.0中,collection最大的一个改变就是可以指定它的具体类型:
List<String> list=new List<String>;

两个最基本的接口:
public interface Collection<E>
{
boolean add(E element);
Iterator<E> iterator();
. . .
}

public interface Iterator<E>
{
E next();
boolean hasNext();
void remove();
}

在5.0以前,常用的形式就是:
Collection<String> c = . . .;
Iterator<String> iter = c.iterator();
while (iter.hasNext())
{
String element = iter.next();
do something with element
}
但是在5.0中加入另外一种循环方式,类似于for each:
for (String element : c)
{
do something with element
}
这种方式对任何实现了Iterable接口的类都适用。

在使用remove的时候特别要注意的一点是,在调用remove之前必须先调用一次next方法,因为next就像是在移动一个指针,remove删掉的就是指针刚刚跳过去的东西。即使是你想连续删掉两个相邻的东西,也必须在每次删除之前调用next。

对collection排序和查找
Collections类的sort方法可以对任何实现了List接口的类进行排序。在排序过程中,他默认这些类实现了Comparable接口,如果想用其他方法排序,可以在调用sort方法的时候提供一个Comparator对象:
Comparator<Item> itemComparator = new
Comparator<Item>()
{
public int compare(Item a, Item b)
{
return a.partNumber - b.partNumber;
}
});
反向排序:
Collections.sort(items, itemComparator);
Collections.sort(items, Collections.reverseOrder(itemComparator));

查找一个对象:
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


wangjiong edited on 2005-11-10 17:35

作者 Re:基于JDK5.0的一些collection类的使用总结 [Re:wangjiong]
逍遥神仙





发贴: 2
积分: 0
于 2005-11-11 19:34 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
good!


作者 Re:基于JDK5.0的一些collection类的使用总结 [Re:wangjiong]
zhaohao





发贴: 3
积分: 0
于 2005-11-13 10:12 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不错


作者 Re:基于JDK5.0的一些collection类的使用总结 [Re:wangjiong]
langhua983





发贴: 17
积分: 0
于 2006-01-08 16:45 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
顶~~~~

还想看些更不错的文章!




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923