边读边写【3】 ----java 集合包之各个集合性能分析

本文对比分析了Java中HashSet与TreeSet的数据结构特点及性能表现,通过不同规模数据集的实验,得出结论:当数据量较大且需频繁进行查找和删除操作时,Set集合相比List更具优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上次主要看了Map接口,以及Map的选择地址:
。[url]http://jiuyuehe.iteye.com/blog/1480386[/url]
这次看的是Set接口。还有一个总结,到这里java常用的集合基本就这些了。总结下,分析分析性能。

[size=medium]Set 跟List 最明显的区别就是 Set 不允许元素重复,而List 可以。
Set中主要是HashSet 跟TreeSet[/size]

[b][size=medium]HashSet [/size][/b]
api 中对它如下解释:

[u]此类实现 Set 接口,由哈希表(实际上是一个 HashMap 实例)支持。它不保证 set 的迭代顺序;特别是它不保证该顺序恒久不变。此类允许使用 null 元素。

此类为基本操作提供了稳定性能,这些基本操作包括 add、remove、contains 和 size,假定哈希函数将这些元素正确地分布在桶中。对此 set 进行迭代所需的时间与 HashSet 实例的大小(元素的数量)和底层 HashMap 实例(桶的数量)的“容量”的和成比例。因此,如果迭代性能很重要,则不要将初始容量设置得太高(或将加载因子设置得太低)。[/u]

从源码中也可以看的出来
  private transient HashMap<E,Object> map;

// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();

/**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<E,Object>();
}


他就是用了一个Map 把key弄能了一个new Object() hash 到他实际的地址中,原来就是HashMap嘛。那具体还是回到了Map中 。[url=http://jiuyuehe.iteye.com/blog/1480386]点击看HashMap[/url]

[color=green]-----------------------------------分割线------------------------------------------------[/color]

[b][size=medium]TreeSet [/size][/b]
这个HashSet 就是由HashMap 实现,TreeSet 是不是TreeMap 呢。

 private static final Object PRESENT = new Object();

/**
* Constructs a set backed by the specified navigable map.
*/
TreeSet(NavigableMap<E,Object> m) {
this.m = m;
}

/**
* Constructs a new, empty tree set, sorted according to the
* natural ordering of its elements. All elements inserted into
* the set must implement the {@link Comparable} interface.
* Furthermore, all such elements must be <i>mutually
* comparable</i>: {@code e1.compareTo(e2)} must not throw a
* {@code ClassCastException} for any elements {@code e1} and
* {@code e2} in the set. If the user attempts to add an element
* to the set that violates this constraint (for example, the user
* attempts to add a string element to a set whose elements are
* integers), the {@code add} call will throw a
* {@code ClassCastException}.
*/
public TreeSet() {
this(new TreeMap<E,Object>());
}


还真是-。那继续[url=http://jiuyuehe.iteye.com/blog/1480386]点击看TreeMap[/url]

[color=green]========================================大分割线=======================================================[/color]
[url=http://jiuyuehe.iteye.com/blog/1480165]点击看List[/url]
[url=http://jiuyuehe.iteye.com/blog/1480386]点击看Map[/url]

这里将java集合包里面常用的集合以及他的实现都过了一下,这些集合各有各的特点,各有各的缺点。那么什么时候选什么集合,这里总结下:

性能测试: 单线程下,测试集合大小分别为100,1000,10000.情况下的add remove get 测试10次取平均.

以下数据来自《分布式java应用》
A=ArrayList L= LinkedList HS= HashSet HM = HashMap TS = TreeSet V =Vector S =Stack TM = TreeMap
元素100时候(单位ns)
集合 增加 查找 删除
A 3756 2873 2444
L 3251 2829 2330
V 2402 2846 2704
S 2180 2572 2277
HS 2930 1559 1720
TS [color=red]5662[/color] 1846 [color=red]4413[/color]
HM 3004 1579 1726
TM [color=red]5410 [/color] 1840 [color=red]4123[/color]


元素1000时候
集合 增加 查找 删除
A 4156 8873 9444
L 3251 [color=red]12829 12330[/color]
V 2802 [color=red]10846 9704[/color]
S 1880 [color=red]10572 9277[/color]
HS 2130 1559 2720
TS 3062 1846 4413
HM 1995 1579 1726
TM 3410 1840 4123


元素10000时候
集合 增加 查找 删除
A [color=red] 8156 88873 99444 [/color]
L 3251 [color=red]152829 152330[/color]
V [color=red] 8802 95846 95704[/color]
S 1880 [color=red]96572 99277[/color]
HS 2130 1559 2720
TS 3062 1846 4413
HM 1995 1579 1726
TM 3410 1840 4123


[size=medium]随着元素的增加 add 所以集合基本变动不大 List还是有所下降。 所有List 的删除,查找 性能 大幅度下降。 Set Map 基本不受元素的数量影响。 [/size]

[b][size=medium][color=red]总结: 对于查找和删除较为频繁,元素量较多的情况(超过10000)Set /Map 才是更好的选择[/color][/size][/b]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值