Hashset与Treeset [英] Hashset vs Treeset
问题描述
我一直很喜欢树木,很好的 O(n * log(n))
以及它们的整洁。但是,我所知道的每个软件工程师都明确地问我为什么要使用 TreeSet
。从CS背景来看,我认为你所使用的并不重要,而且我不关心哈希函数和桶(在 Java $ c $的情况下) c>)。
I've always loved trees, that nice O(n*log(n))
and the tidiness of them. However, every software engineer I've ever known has asked me pointedly why I would use a TreeSet
. From a CS background, I don't think it matters all that much which you use, and I don't care to mess around with hash functions and buckets (in the case of Java
).
在哪种情况下,我应该在 TreeSet上使用
HashSet
/ code>?
In which cases should I use a HashSet
over a TreeSet
?
推荐答案
HashSet比TreeSet快得多(常量时间与日志时间对比为大多数操作,如添加,删除和包含)但不提供像TreeSet这样的排序保证。
- 该类为基本操作提供恒定的时间性能(添加,删除,包含和大小)。
- 它不保证元素的顺序会随时间保持不变
- 迭代性能取决于初始容量和HashSet的加载因子。
- 接受默认加载因子是非常安全的,但您可能希望指定的初始容量大约是您希望该组增长的大小的两倍。
- the class offers constant time performance for the basic operations (add, remove, contains and size).
- it does not guarantee that the order of elements will remain constant over time
- iteration performance depends on the initial capacity and the load factor of the HashSet.
- It's quite safe to accept default load factor but you may want to specify an initial capacity that's about twice the size to which you expect the set to grow.
- 保证日志(n)基本操作的时间成本(添加,删除和包含)
- 保证set的元素将被排序(升序,自然或您通过其构造函数指定的元素) (实现
SortedSet
) - 不提供迭代性能的任何调整参数
- 提供一些方便的方法来处理订购像
首先设置()
,最后()
,headSet()
,tailSet()
等
- guarantees log(n) time cost for the basic operations (add, remove and contains)
- guarantees that elements of set will be sorted (ascending, natural, or the one specified by you via its constructor) (implements
SortedSet
) - doesn't offer any tuning parameters for iteration performance
- offers a few handy methods to deal with the ordered set like
first()
,last()
,headSet()
, andtailSet()
etc
- 两者都保证元素的无重复收集
- 将元素添加到HashSet然后将集合转换为TreeSet以进行无重复的排序遍历通常会更快。
- 这些实现都不会同步。也就是说,如果多个线程同时访问一个集合,并且至少有一个线程修改了集合,则必须在外部进行同步。
- LinkedHashSet 在某种意义上介于
HashSet
和TreeSet
之间。实现为具有贯穿其的链表的哈希表,然而,它提供了插入顺序的迭代,这与TreeSet保证的排序遍历不同。
- Both guarantee duplicate-free collection of elements
- It is generally faster to add elements to the HashSet and then convert the collection to a TreeSet for a duplicate-free sorted traversal.
- None of these implementations are synchronized. That is if multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.
- LinkedHashSet is in some sense intermediate between
HashSet
andTreeSet
. Implemented as a hash table with a linked list running through it, however,it provides insertion-ordered iteration which is not same as sorted traversal guaranteed by TreeSet.
因此,使用选择完全取决于您的需求,但我觉得即使您需要有序集合,您仍然应该更喜欢HashSet来创建Set然后将其转换为TreeSet。
So a choice of usage depends entirely on your needs but I feel that even if you need an ordered collection then you should still prefer HashSet to create the Set and then convert it into TreeSet.
- 例如
的SortedSet<字符串> s = new TreeSet< String>(hashSet);
- e.g.
SortedSet<String> s = new TreeSet<String>(hashSet);
这篇关于Hashset与Treeset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!