1.TreeSet principle:
/*
* TreeSet can be sorted when storing objects, but it is necessary to specify an algorithm for sorting
* * Integer can be sorted (with default order), string can be sorted (with default order), the custom class is stored with an exception (no order)
* * If you want to sort the objects of a custom class into TreeSet, you must implement the comparable interface
* Implement comparable * override CompareTo () method on class
* Define the comparison algorithm within the method, return positive negative numbers or 0 depending on the size relationship
* When using TreeSet to store objects, the Add () method automatically calls the CompareTo () method for comparison, and is stored using a two-fork tree as the result of the comparison.
*/
2.TreeSet relies on treemap to achieve this. TreeSet is an ordered set, and the elements in the TreeSet are arranged in ascending order, by default, by natural ordering, meaning that the elements in treeset implement comparable interfaces. Or there is a custom comparer. We can pass a comparer object that implements the comparator interface when constructing a TreeSet object.
import Java.util.iterator; Import Java.util.*; Public class Treesettest { public static void< Span style= "color: #000000;" > main (string[] args) {Set ts = new TreeSet (); Ts.add ("ABC "); Ts.add ("xyz" Ts.iterator (); while
Output Result:
Abc
Rst
Xyz
The printed result is not the same as the order in which it was previously added, it is sorted by a single letter sorting method. This is because the string class implements the comparable interface.
If the object of a class that we define ourselves is to be added to the TreeSet, then this class must implement the comparable interface.
PackageTest.treeset;ImportJava.util.Iterator;ImportJava.util.Set;ImportJava.util.TreeSet;PublicClassTest_treeset {@SuppressWarnings ("unchecked")PublicStaticvoidMain (string[] args) {Set ts =NewTreeSet (); Ts.add (New Teacher ("Zhangsan", 1)); Ts.add (New Teacher ("Lisi", 2)); Ts.add (New Teacher ("Wangmazi", 3)); Ts.add (New Teacher ("Wangwu", 4)); Ts.add (New Teacher ("Mazi", 3)); Iterator it =Ts.iterator ();While(It.hasnext ()) {System.out.println (It.next ());} }}Class TeacherImplementsComparable {IntNum String name; Teacher (String name,IntNUM) {This.num =NumTHIS.name =Name }PublicString toString () {Return "study number:" + num + "\t\t Name:" +Name }//o, starting from the root node public int CompareTo ( Object o) {Teacher ss = (Teacher) O; int result = num < ss.num? 1: (num = = ss.num? 0:-1); // descending //int result = num > ss.num? 1: (num = = ss.num? 0:-1); //if (Result = = 0< Span style= "color: #000000;" ) {result = Name.compareto (ss.name);} return
Operation Result:
School Number: 4 Name: WANGWU No.: 3 Name: Mazi No.: 3 Name: Wangmazi No.: 2 Name: Lisi No.: 1 Name: Zhangsan
3. Comparator
You can pass a comparer when you use arrays to sort the elements in an array.
You can pass a comparer when you use collections to sort the elements in the collection.
Can you pass in a comparer when you use TreeSet to sort the elements that are added to them?
Super E> Comparator) {This (new treemap<e,object>(Comparator));}
By looking at how it is constructed, you know that you can pass in a comparer.
Constructs a new empty treeset that is sorted according to the specified comparer. All elements inserted into the set must be able to be compared by the specified comparer: for any two elements in the set E1 and E2, the execution Comparator.compare (E1, E2) should not throw classcastexception. If the user attempts to add an element that violates this constraint to the set, the add call throws ClassCastException.
PackageTest.treeset;ImportJava.util.Comparator;ImportJava.util.Iterator;ImportJava.util.TreeSet;PublicClasstreesettest {@SuppressWarnings ("unchecked")PublicStaticvoidMain (string[] args) {TreeSet ts =New TreeSet (NewTeacher2.teachercompare ()); Ts.add (New Teacher2 ("Zhangsan", 2)); Ts.add (New Teacher2 ("Lisi", 1)); Ts.add (New Teacher2 ("Wangmazi", 3)); Ts.add (New Teacher2 ("Mazi", 3)); Iterator it =Ts.iterator ();While(It.hasnext ()) {System.out.println (It.next ());} }}ClassTeacher2 {IntNum String name; Teacher2 (String name,IntNUM) {This.num =NumTHIS.name =Name }PublicString toString () {Return "study number:" + num + "Name:" +Name }StaticClass TeachercompareImplements Comparator {//A comparator that the teacher comes with//o1 the object node //o2, starting from the root node to compare public int Compare (object O1, Object O2) {Teacher2 S1 = (Teacher2) o1; transformation Teacher2 s2 = (Teacher2) O2; // transition int result = S1.num > S2.num? 1: (S1.num = = s2.num? 0: -1if (Result = = 0 S1.name.compareTo (s2.name);} return result;}}
Operation Result:
School Number: 1 Name: Lisi No.: 2 Name: Zhangsan No.: 3 Name: Mazi No.: 3 Name: Wangmazi
TreeSet principle:
The principle is not the same, but the efficiency has a certain difference
Article posted: http://www.cnblogs.com/meng72ndsc/archive/2010/12/23/1914477.html
TreeSet and TreeMap sort