Treeset () Class
The treeset () class implements the set interface, which is supported by the treemap instance. This class ensures that sorted sets arrange elements in ascending order, and mayNatural orderSort (seeComparable), Or sort by the comparator provided when the set is created.TreesetThe instance will use itsCompareto(OrCompare) Method to execute all key comparisons. Therefore, from the set perspective, this method considers the two keys that are equal to each other to be equal. Even if the order of the set is different from the equal sign, its behavior is alsoYesWell defined; it just violatesSetThe common protocol of the API.
1
PublicTreeset()
Construct a new empty set, which is sorted by the natural order of elements. All elements inserted into the set must implement the comparable interface.
public boolean add(E o)
When an element is added, the comparable API is called to compare the elements. The O element is added to the corresponding position and maintained in ascending order.
2
Rewrite class, implement the comparator interface, and implement the compare method internally;
PublicTreeset(Comparator <? Super E> C)
Creates a new empty set, which is sorted by the specified comparator. All elements inserted into the set must be compared by the specified comparator.