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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:44:53  来源:igfitidea点击:

Java: Hashset Vs TreeSet when should i use over other

javacollections

提问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:

举个例子:

  1. 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)

  2. 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).

  1. 我有一个可比较的对象。我已经把它们放在 HashSet 中。现在当(仅当我想要)我想设置为基于 compareTo 逻辑排序时,我可以调用 Collections.sort(object)

  2. 而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 HashSetand TreeSetis performance. HashSetis faster than TreeSetand should be preferred choice if sorting of element is not required.

1)HashSet和之间的第一个主要区别TreeSet是性能。如果不需要元素排序,HashSet则比它更快TreeSet并且应该是首选。

2) Second difference between HashSetand TreeSetis that HashSetallows null object but TreeSetdoesn't allow null Object and throw NullPointerException, Why, because TreeSetuses compareTo()method to compare keys and compareTo()will throw java.lang.NullPointerException.

2) HashSetand之间的第二个区别TreeSetHashSet允许空对象但TreeSet不允许空对象和 throw NullPointerException,为什么,因为TreeSet使用compareTo()方法比较键compareTo()并将 throw java.lang.NullPointerException

3) Another significant difference between HashSetand TreeSetis that , HashSetis backed by HashMapwhile TreeSetis backed by NavigableMap in Java.

3)之间的另一个显著差异HashSetTreeSet是,HashSet被支持HashMap,同时TreeSet由NavigableMap的使用Java支持。

4) One more difference between HashSetand TreeSetwhich is worth remembering is that HashSet uses equals()method to compare two object in Set and for detecting duplicates while TreeSetuses 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 HashSetand TreeSetis ordering. HashSetdoesn't guaranteed any order while TreeSetmaintains objects in Sorted order defined by either Comparableor Comparatormethod in Java.

5) 现在HashSet和之间最重要的区别TreeSet是排序。HashSet同时不保证任何顺序TreeSet在由任一定义的排序顺序保持的对象ComparableComparator在Java方法。

6) TreeSetdoes not allow to insert Heterogeneousobjects. It will throw classCastExceptionat Runtimeif trying to add hetrogeneous objects, whereas HashSetallows hetrogeneous objects.

6)TreeSet不允许插入Heterogeneous对象。这将引发classCastExceptionRuntime,如果尝试添加hetrogeneous对象,而HashSet允许hetrogeneous对象。