排序之HashSet和TreeSet的区别

[size=medium] 众所周知,java中Set里的数据不可重复,并且具有排序性,当我们在项目中遇到需要去重复并且排序的需求时难免想起Set。Set的实现类中HashSet和TreeSet在我们的工作中使用最为频繁,HashSet要首当其冲,但是它并非万能的哦,同样是既要去重又要排序,但两者的区别就体现出来了。咱们看代码先:[/size]

public static void main(String[] args) {
Set<CnfmSelectItem> hashSet = new HashSet<CnfmSelectItem>();
CnfmSelectItem item = new CnfmSelectItem("(GP) Other Forecasted Item - CDM / DLN / RDLN");
CnfmSelectItem item1 = new CnfmSelectItem("(GP) Other Forecasted Item - Freight");
CnfmSelectItem item2 = new CnfmSelectItem("(GP) Other Forecasted Item - LES: Magic");
CnfmSelectItem item3 = new CnfmSelectItem("(GP) Other Forecasted Item - Resident TA");
CnfmSelectItem item4 = new CnfmSelectItem("(GP) Other Forecasted Item - TIL");
CnfmSelectItem item5 = new CnfmSelectItem("(GP) Other Forecasted Item - CDM / DLN / RDLN");
CnfmSelectItem item6 = new CnfmSelectItem("(GP) Other Forecasted Item - Resident TA");

hashSet.add(item);
hashSet.add(item1);
hashSet.add(item2);
hashSet.add(item3);
hashSet.add(item4);
hashSet.add(item5);
hashSet.add(item6);
System.out.println("***************This result using the HashSet collection.**************************\n");
for (CnfmSelectItem cnfmSelectItem : hashSet) {
System.out.println(cnfmSelectItem.getValue());
}
System.out.println("\n***************This result using the TreeSet collection.**************************\n");
Set<CnfmSelectItem> treeSet = new TreeSet<CnfmSelectItem>();
treeSet.addAll(hashSet);
for (CnfmSelectItem cnfmSelectItem : treeSet) {
System.out.println(cnfmSelectItem.getValue());
}
}


[size=medium] CnfmSelectItem是javax.faces.model.SelectItem的封装类,在jsf框架中使用比较广泛,这里就不多说了。[/size]

***************This result using the HashSet collection.**************************

(GP) Other Forecasted Item - LES: Magic
(GP) Other Forecasted Item - Resident TA
(GP) Other Forecasted Item - CDM / DLN / RDLN
(GP) Other Forecasted Item - TIL
(GP) Other Forecasted Item - Freight

***************This result using the TreeSet collection.**************************

(GP) Other Forecasted Item - CDM / DLN / RDLN
(GP) Other Forecasted Item - Freight
(GP) Other Forecasted Item - LES: Magic
(GP) Other Forecasted Item - Resident TA
(GP) Other Forecasted Item - TIL


[size=medium]从运行结果中不难看出HashSet和TreeSet的区别,也发现TreeSet在排序时更给力。

下列总结来自互联网:
HashSet
此类实现 Set 接口,由哈希表(实际上是一个 HashMap 实例)支持。它不保证集合的迭代顺序;特别是它不保证该顺序恒久不变。此类允许使用 null 元素。
此类为基本操作提供了稳定性能,这些基本操作包括 add、remove、contains 和 size,假定哈希函数将这些元素正确地分布在桶中。对此集合进行迭代所需的时间与 HashSet 实例的大小(元素的数量)和底层 HashMap 实例(桶的数量)的“容量”的和成比例。因此,如果迭代性能很重要,则不要将初始容量设置得太高(或将加载因子设置得太低)。
我们应该为要存放到散列表的各个对象定义hashCode()和equals();


TreeSet
此类实现 Set 接口,该接口由 TreeMap 实例支持。此类保证排序后的 set 按照升序排列元素,根据使用的构造方法不同,可能会按照元素的自然顺序 进行排序,或按照在创建 set 时所提供的比较器进行排序。
是一个有序集合,元素中安升序排序,缺省是按照自然顺序进行排序,意味着TreeSet中元素要实现Comparable接口;
我们可以构造TreeSet对象时,传递实现了Comparator接口的比较器对象.

相信大家对HashSet和TreeSet有了更深层的了解,工作中也能更好的使用它们啦。[/size]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值