Just wondering what the pros and cons of a TreeSet is, if anyone could tell me please? Thanks!
-
these questions and corresponding answers deserve to be out there. very helpful! thank you! – mtical Jun 14 '14 at 03:48
5 Answers
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.

- 2,426
- 2
- 23
- 34
-
4Typical use case: Take a text, drop all words from this text in a TreeSet
and the iterator provides an alphabetically sorted word index (with no duplicates). – Andreas Dolk Aug 19 '09 at 07:27 -
"Use HashSet when you don’t need sequential access, just lookup by key. " - You mean when you don't need lookup by key (which is what a Map provides)? – Bart van Heukelom Nov 24 '11 at 12:11
-
If you need a specific sequential access but no sorting you may use LinkedHashSet that preserves the order of input data. Also TreeSet has remove order of logn while PriorityQueue remove order is O(n) – Masood_mj Jul 29 '12 at 22:30
-
Optionally, you can construct a TreeSet with a constructor that lets you give the collection your own rules for what the order should be (rather than relying on the ordering defined by the elements' class) by using a Comparable or Comparator. As of Java 6, TreeSet implements NavigableSet. So no obligation to use Comparable or comparator – Timo Oct 15 '13 at 18:18
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.

- 7,088
- 3
- 24
- 38
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.

- 5,773
- 3
- 40
- 49
-
Seems odd to metion "based on a red/black tree algorithm" as a "pro". Also seems a bit weird to mention O(log(N)) complexity as a pro when the obvious thing to compare TreeSet to is HashSet which (as you said) has better time complexity. – Laurence Gonsalves Aug 19 '09 at 06:50
-
1@Laurence: 1) Insertion into a List is O(N) if you include checking for uniqueness, so O(log(N)) is a "pro" relative to that. 2) a HashSet is only O(1) if you use a decent hash function and you allow the hash table to grow. – Stephen C Aug 19 '09 at 06:59
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.

- 1,081
- 2
- 9
- 14
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.

- 3,982
- 4
- 40
- 46
-
1-1: HashSet uses a red-black tree. According to [this page](http://www.eli.sdsu.edu/courses/fall95/cs660/notes/RedBlackTree/RedBlack.html) the complexity of delete is O(log(N)) – Stephen C Aug 19 '09 at 07:06