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.
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.
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.
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.
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.
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.
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.
TreeSet: Pros: sorted, based on a red/black tree algorithm, provides O(log(N)) complexity for operations. Cons: value must either be Comparable or you need to provide Comparator in the constructor. Moreover, the HashSet implementation provides better performance as it provides ~O(1) complexity.
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.
Often you can use PriorityQueue instead of TreeSet. And in your typical use case it's better just to sort the array of strings.
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.
发布评论
评论(5)
Collection 类之一。它允许您按键或按顺序按顺序访问集合中的元素。它的开销比 ArrayList 或 HashMap 要多得多。当您不需要顺序访问而只需按键查找时,请使用 HashSet。使用 ArrayList 并使用数组。如果您只想按顺序排列元素,请排序。 TreeSet 始终保持元素有序。使用 ArrayList,您只需在需要时进行排序即可。
对于 TreeSets,键必须嵌入到存储在集合中的对象中。通常您可能有字符串的 TreeSet。然后你所能做的就是判断给定的字符串是否在集合中。它不会像树状图那样找到关联对象。对于 TreeMap,键和它们关联的对象是分开的。
奇怪的是,TreeSet 及其兄弟 TreeMap 与表示树无关。在内部,他们使用树形组织为您提供按字母顺序排序的集合/地图,但您无法控制父母和孩子之间的链接。
TreeSet 内部使用红黑树。无需对数据进行预排序即可获得平衡良好的树。另一方面,如果数据被排序(升序或降序),则不会像其他一些类型的树那样造成损害。
如果您不提供 Comparator 来定义所需的顺序,TreeSet 需要在项目类上实现 Comparable 来定义自然顺序。
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.
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.
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.
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.
缺点:TreeSet 的一个缺陷是它以一种意想不到的方式实现 Set 接口。
如果 TreeSet 包含对象 a,则如果 a.compareTo(b) 返回 0,则对象 b 被视为集合的一部分,即使 a.equals(b) 为 false,因此如果compareTo 和 equals 未以一致的方式实现,你的处境会很糟糕。
当方法返回 Set 并且您不知道实现是 TreeSet 还是 HashSet 时,这尤其是一个问题。
这里要吸取的教训是,始终避免不一致地实现compareTo和equals。如果需要以与 equals 不一致的方式对对象进行排序,请使用比较器。
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.
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.
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.
树集:
优点:基于红/黑树算法进行排序,为操作提供 O(log(N)) 复杂度。
缺点:值必须是Comparable,或者您需要在构造函数中提供Comparator。此外,HashSet 实现提供了更好的性能,因为它的复杂度约为 O(1)。
TreeSet:
Pros: sorted, based on a red/black tree algorithm, provides O(log(N)) complexity for operations.
Cons: value must either be Comparable or you need to provide Comparator in the constructor. Moreover, the HashSet implementation provides better performance as it provides ~O(1) complexity.
TreeSet 会产生内存碎片,并且有额外的内存开销。您可以查看源并计算附加内存量和它创建的附加对象量。当然,这取决于存储对象的性质,你也可以怀疑我对内存有偏执:)但最好不要到处花——你有GC,你有缓存未命中,所有这些事情都很慢。
通常您可以使用 PriorityQueue 而不是 TreeSet。在您的典型用例中,最好对字符串数组进行排序。
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.
Often you can use PriorityQueue instead of TreeSet. And in your typical use case it's better just to sort the array of strings.
我猜想这个数据结构将使用二叉树来维护数据,以便可以进行升序检索。在这种情况下,如果它试图保持树平衡,那么删除操作的成本会有点高。
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.