java TreeSet 的优缺点是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1298144/
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
What are the pros and cons of a TreeSet
提问by Rifk
Just wondering what the pros and cons of a TreeSet is, if anyone could tell me please? Thanks!
只是想知道 TreeSet 的优缺点是什么,如果有人能告诉我吗?谢谢!
回答by RubyDubee
One of the Collection classes. It lets you access the elements in your collection by key, or sequentially by key. It has considerably more overhead than ArrayList or HashMap. Use HashSet when you don't need sequential access, just lookup by key. Use an ArrayList and use Arrays. sort if you just want the elements in order. TreeSet keeps the elements in order at all times. With ArrayList you just sort when you need to. With TreeSets the key must be embedded in the object you store in the collection. Often you might have TreeSet of Strings. All you can do then is tell if a given String is in the Set. It won't find you an associated object he way a Treemap will. With a TreeMap the keys and the objects they are associated with are separate.
Collection 类之一。它允许您按键或按顺序依次访问集合中的元素。它的开销比 ArrayList 或 HashMap 多得多。当您不需要顺序访问时使用 HashSet,只需按键查找即可。使用 ArrayList 和使用数组。如果您只想按顺序排列元素。TreeSet 始终保持元素有序。使用 ArrayList 您只需在需要时进行排序。使用 TreeSet,键必须嵌入到您存储在集合中的对象中。通常你可能有字符串的 TreeSet。你所能做的就是判断给定的字符串是否在集合中。它不会像 Treemap 那样为您找到关联对象。使用 TreeMap,键和它们关联的对象是分开的。
TreeSet and its brother TreeMap oddly have nothing to do with representing trees. Internally they use a tree organisation to give you an alphabetically sorted Set/Map, but you have no control over links between parents and children.
TreeSet 和它的兄弟 TreeMap 奇怪地与表示树无关。在内部,他们使用树状组织为您提供按字母顺序排序的 Set/Map,但您无法控制父级和子级之间的链接。
Internally TreeSet uses red-black trees. There is no need to presort the data to get a well-balanced tree. On the other hand, if the data are sorted (ascending or descending), it won't hurt as it does with some other types of tree.
TreeSet 内部使用红黑树。无需对数据进行预排序以获得平衡良好的树。另一方面,如果数据被排序(升序或降序),它不会像其他一些类型的树那样受到伤害。
If you don't supply a Comparator to define the ordering you want, TreeSet requires a Comparable implementation on the item class to define the natural order.
如果您不提供 Comparator 来定义所需的排序,则 TreeSet 需要在项目类上使用 Comparable 实现来定义自然顺序。
回答by Buhb
Cons: One pitfall with TreeSet is that it implements the Set interface in an unexpected way. If a TreeSet contains object a, then object b is considered part of the set if a.compareTo(b) returns 0, even if a.equals(b) is false, so if compareTo and equals isn't implemented in a consistent way, you are in for a bad ride.
缺点:TreeSet 的一个缺陷是它以一种意想不到的方式实现了 Set 接口。如果 TreeSet 包含对象 a,则如果 a.compareTo(b) 返回 0,则对象 b 被视为集合的一部分,即使 a.equals(b) 为 false,因此如果 compareTo 和 equals 未以一致的方式实现,你的旅程很糟糕。
This is especially a problem when a method returns a Set, and you don't know if the implementation is a TreeSet or, for instance, a HashSet.
当一个方法返回一个 Set 而你不知道实现是一个 TreeSet 还是一个 HashSet 时,这尤其是一个问题。
The lesson to learn here is, always avoid implementing compareTo and equals inconsistently. If you need to order objects in a way that is inconsistent with equals, use a Comparator.
这里要学习的教训是,始终避免不一致地实现 compareTo 和 equals。如果您需要以与 equals 不一致的方式对对象进行排序,请使用 Comparator。
回答by Shimi Bandiel
TreeSet:
Pros: sorted, based on a red/black tree algorithm, provides O(log(N)) complexity for operations.
Cons: value must either be Comparableor you need to provide Comparatorin the constructor. Moreover, the HashSet implementation provides better performance as it provides ~O(1) complexity.
TreeSet:
优点:排序,基于红/黑树算法,为操作提供 O(log(N)) 复杂度。
缺点: value 必须是Comparable或者您需要在构造函数中提供Comparator。此外,HashSet 实现提供了更好的性能,因为它提供了 ~O(1) 复杂度。
回答by Vitaly
TreeSet fragments memory and has additional memory overheads. You can look at the sources and calculate amount of additional memory and amount of additional objects it creates. Of course it depends on the nature of stored objects and you also can suspect me to be paranoiac about memory :) but it's better to not spend it here and there - you have GC, you have cache misses and all of these things are slooow.
TreeSet 将内存分段并具有额外的内存开销。您可以查看源并计算它创建的额外内存量和额外对象量。当然,这取决于存储对象的性质,您也可以怀疑我对内存有偏执狂:) 但最好不要到处花它 - 您有 GC,您有缓存未命中,所有这些事情都很慢。
Often you can use PriorityQueue instead of TreeSet. And in your typical use case it's better just to sort the array of strings.
通常您可以使用 PriorityQueue 而不是 TreeSet。在您的典型用例中,最好只对字符串数组进行排序。
回答by jatanp
I guess this datastructure would be using binary tree to maintain data so that ascending order retrieval is possible. In that case, if it tries to keep the tree in balance then the remove operation would be bit costly.
我猜这个数据结构将使用二叉树来维护数据,以便可以进行升序检索。在这种情况下,如果它试图保持树的平衡,那么移除操作的成本会有点高。

