Elements that are added into TreeSet must be Comparable, implementing Comparable interface; or transferring a Comparator that implements Comparator interface via TreeSet argument, i.e.
public TreeSet(Comparator comparator)
The underlying reason; TreeSet is implemented as a black-red tree, it always compares two elements, and knowing bigger or smaller in order to put a bigger one in the right sub-tree of a root node, but a smaller one on the left side.
If you don't add a comparable element in the TreeSet, then getting a compiling error.
No comments:
Post a Comment