*treeset compared to hashset, the tree set is an ordered set, and for the tree set traversal, each value is automatically rendered in sorted Order.
* TreeSet is currently using a red-black tree, each time an element is added to the tree, it will be placed in the correct position
*
* Add elements to treeset faster than arrays and linked lists, but slower than hash table (HashSet)
*
* TreeSet by default, Assuming that the inserted element implements the comparable interface, the interface value returns a negative number indicating that a is before B
*
* Public Interface Comparable<t>
* {
* int CompareTo (T other);
* }
*
* Sorting defined using the comparable interface has its limitations
* If you want to insert a custom object, you must implement the comparable interface definition order
* Here's a question to think about, if you need to follow one rule in one set, but use another in another
* And what if you want to sort an object of a class that does not implement the comparable interface?
*
* At this point, you can pass the comparator object to the TreeSet constructor to tell the use of the permutation rule
*
* Public Interface Comparator<t>
* {
* int Compara (t a, t b);
* }
*
1 //Example2 classMycomparatorImplementsComparator<string>{3 4 @Override5 public intCompare (string o1, string O2) {6 if(o1.length () >O2.length ()) {7 return1;8}Else if(o1.length () <O1.length ()) {9 return-1;Ten}Else{ one return0; a } - } - }; the - //the constructed comparer is then passed in -Mycomparator my =NewMycomparator (); -Treeset<string> tree =NewTreeset<> (my);
* If you sort by descriptive information like an example, then pass the object of the class to the constructor of the tree set, so that a tree with a comparison is constructed
* Note that this comparator does not have any data, it is just the holder of the comparison method (sometimes called this object as a function Object)
* Function objects are usually dynamically defined, i.e. anonymous internal classes are often used, such as Example 2
*
//Example 2Treeset<student> Stutree =NewTreeset<student> (NewComparator<student>() {@Override public intCompare (Student o1, Student O2) {if(o1.getbook (). GetPrice () >O2.getbook (). GetPrice ()) { return-1; } Else if(o1.getbook (). GetPrice () <o2. getBook (). getprice ()) { return1; } Else { return0; } }});
* If sorting is not required, it is not necessary to sort the cost, and for some data it is more difficult to sort it than a hash function
*
First knowledge of Java Collection--tree set