Package Treesettest;import Java.util.Iterator; Import Java.util.treeset;import javax.management.runtimeerrorexception;/* can sort the elements in the set set, the underlying data structure is a two-fork tree, The basis for ensuring element uniqueness is CompareTo and return 0; The first way to treeset the ordering of elements is to implement the comparable interface, which overrides the CompareTo method, which also becomes the natural order of the elements, or the second way of ordering TreeSet the default order when the element itself is not comparable. Or the comparison is not required at this point, you need to make the collection itself comparative in the collection initialization, there is a comparative way to remember: when the comparison, if the main conditions are the same, compared to the secondary condition */class Student implements comparable// The interface forces the object to have a comparison of {private String name;private int age; Student (String Name,int age) {this.name = Name;this.age = age;} public int compareTo (Object obj) {/*if (!) ( obj instanceof Student) {throw new RuntimeException ("Not student Object");} Student s = (Student) obj;//System.out.println (this.name+ "=====compare=====" +this.age); if (This.age >s.age) { return 1;} else if (this.age = = s.age) {//If the same age compares name return This.name.compareTo (s.name);} else {return-1;} */return-1;} Public String GetName () {return this.name;} public int getage () {return age;}} public class Treesetdemo{public static void Main (string[] args) {Treeset<studenT> tsset =new treeset<student> () tsset.add (New Student ("as", N)); Tsset.add (New Student ("Fvhgj", 45)); Tsset.add (New Student ("ERT"), Tsset.add (New Student ("Wenjia"), Tsset.add (New Student ("Wejia", 1)); Iterator <Student> iterator = Tsset.iterator (); while (Iterator.hasnext ()) {Student Stu = (Student) iterator.next (); System.out.println (Stu.getage () + "======" +stu.getname ());}}} Package treesettest; Import Java.util.comparator;import Java.util.iterator;import java.util.treeset;/* When the element itself is not comparable, or the comparison is not required. At this point the container itself needs to be comparative. A comparer is defined that passes the comparer object as a parameter to the constructor of the TreeSet collection. When both sorts are present, a comparator is defined as a class, implementing the comparator interface, overriding the Compare method the binary tree takes return 0; Determines whether the elements are equal */public class Treesetdemotwo{public static void Main (string[] args) {treeset<student> tsset = new treeset<student> (new Mycompare ()); Tsset.add (new Student ("wenjai10"), Tsset.add (New Student ("Wenjai11"), Tsset.add (New Student ("wenjai12"); tsset.aDD ("wenjai13", "Student"), Tsset.add (New Student ("Wenjai11"));iterator<student> it = Tsset.iterator (); while (It.hasnext ()) {Student Student = (Student) it.next (); System.out.println (Student.getname () + "======" +student.getage ());}}} Class Mycompare implements Comparator{public int compare (Object o1,object O2) {Student S1 = (Student) O1; Student s2 = (Student) o2;int num = S1.getname (). CompareTo (S2.getname ()); if (num = = 0) {//Because the integer type also has a comparison method, the integer is encapsulated as an object return NE W Integer (S1.getage ()). CompareTo (New Integer (S2.getage ())),/*if (S1.getage () >s2.getage ()) {return 1;} if (s1.getage () = = S2.getage ()) {return 0;} Return-1;*/}return num;}} /////////////////////////////////////////////////////////////////////////////////////////////////////////////// Package Treesettest;import Java.util.comparator;import Java.util.iterator;import java.util.treeset;/* Exercise: Sorting strings by string length is inherently comparative, but it is not comparable to what is needed at this point only the comparator */public class Treesetpractice_one{public static void Main (string[] args) {Treeset<string> ts = new treeset<string> (new Stringlencompare ()), Ts.add ("ASD"), Ts.add ("BB"), Ts.add ("ba"); Ts.add (" Ityior "), Ts.add (" Hysdhbgg ");iterator<string> it = Ts.iterator (); for (String string:ts) {System.out.println ( string);}}} Class Stringlencompare implements comparator{@Overridepublic int Compare (object O1, Object O2) {//TODO auto-generated met Hod stubstring str1 = (String) O1; String str2 = (string) o2;/*if (Str1.length () >str2.length ()) {return 1;} if (str1.length () = = Str2.length ()) {return 0;} */int num = new Integer (Str1.length ()). CompareTo (New Integer (Str2.length ())), if (num = = 0) {return Str1.compareto (STR2);} return num;//return 0;}}
Java TreeSet Collection Sorting && defining a class implementation comparator interface, overriding compare methods && sorting by string length