Java:Hashset Vs TreeSet 我什么时候应该使用其他的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25602382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Hashset Vs TreeSet when should i use over other
提问by SR Nathan
All I have been reading lots of blogs on this subject but still I could not get clear idea when to use one over another hashset or tree set.
我一直在阅读大量关于这个主题的博客,但我仍然无法清楚地知道何时使用另一个哈希集或树集。
Taken an example:
举个例子:
I have a comparable objects. I have put them in HashSet. Now when (only when I want) i want to set to be sorted based on compareTo logic, I can call Collections.sort(object)
whereas Treeset by default always use the compareTo or compare(obj1, obj2) all the time. Hence performance will be hit with TreeSet but output will be same as #1 (Collections.sort).
我有一个可比较的对象。我已经把它们放在 HashSet 中。现在当(仅当我想要)我想设置为基于 compareTo 逻辑排序时,我可以调用 Collections.sort(object)
而TreeSet的默认一直使用的compareTo或比较(OBJ1,OBJ2)所有的时间。因此,TreeSet 会影响性能,但输出将与 #1 (Collections.sort) 相同。
Is this understanding correct?
这种理解是否正确?
采纳答案by Ankur Singhal
HashSetis Implemented using a hash table. Elements are not ordered. The add, remove,
and contains methods have constant time complexity O(1).
HashSet是使用哈希表实现的。元素没有顺序。在add, remove,
和包含的方法有固定的时间复杂度为O(1) 。
TreeSetis implemented using a tree structure(red-black tree in algorithm book). The elements in a set are sorted, but the add, remove, and contains methods has time complexity O(log (n)). It offers several methods to deal with the ordered set like first(), last(), headSet(), tailSet()
, etc.
TreeSet是使用树结构(算法书中的红黑树)实现的。集合中的元素已排序,但 add、remove 和 contains 方法的时间复杂度为 O(log (n))。它提供了几种处理有序集的方法,例如first(), last(), headSet(), tailSet()
等。
1) First major difference between HashSet
and TreeSet
is performance. HashSet
is faster than TreeSet
and should be preferred choice if sorting of element is not required.
1)HashSet
和之间的第一个主要区别TreeSet
是性能。如果不需要元素排序,HashSet
则比它更快TreeSet
并且应该是首选。
2) Second difference between HashSet
and TreeSet
is that HashSet
allows null object but TreeSet
doesn't allow null Object and throw NullPointerException
, Why, because TreeSet
uses compareTo()
method to compare keys and compareTo()
will throw java.lang.NullPointerException
.
2) HashSet
and之间的第二个区别TreeSet
是HashSet
允许空对象但TreeSet
不允许空对象和 throw NullPointerException
,为什么,因为TreeSet
使用compareTo()
方法比较键compareTo()
并将 throw java.lang.NullPointerException
。
3) Another significant difference between HashSet
and TreeSet
is that , HashSet
is backed by HashMap
while TreeSet
is backed by NavigableMap in Java.
3)之间的另一个显著差异HashSet
和TreeSet
是,HashSet
被支持HashMap
,同时TreeSet
由NavigableMap的使用Java支持。
4) One more difference between HashSet
and TreeSet
which is worth remembering is that HashSet uses equals()
method to compare two object in Set and for detecting duplicates while TreeSet
uses compareTo()
method for same purpose. if equals()
and compareTo()
are not consistent, i.e. for two equal object equals should return true while compareTo()
should return zero, than it will break contract of Set interface and will allow duplicates in Set implementations like TreeSet
4)HashSet
和之间还有TreeSet
一个值得记住的区别是 HashSet 使用equals()
方法比较 Set 中的两个对象并检测重复项,而TreeSet
使用compareTo()
方法用于相同目的。如果equals()
和compareTo()
不一致,即对于两个相等的对象,equals 应该返回 true 而compareTo()
应该返回零,这会破坏 Set 接口的契约,并允许在 Set 实现中重复,例如 TreeSet
5) Now most important difference between HashSet
and TreeSet
is ordering. HashSet
doesn't guaranteed any order while TreeSet
maintains objects in Sorted order defined by either Comparable
or Comparator
method in Java.
5) 现在HashSet
和之间最重要的区别TreeSet
是排序。HashSet
同时不保证任何顺序TreeSet
在由任一定义的排序顺序保持的对象Comparable
或Comparator
在Java方法。
6) TreeSet
does not allow to insert Heterogeneous
objects. It will throw classCastException
at Runtime
if trying to add hetrogeneous objects, whereas HashSet
allows hetrogeneous objects.
6)TreeSet
不允许插入Heterogeneous
对象。这将引发classCastException
的Runtime
,如果尝试添加hetrogeneous对象,而HashSet
允许hetrogeneous对象。