TreeSet: You can sort the elements in the Set collection
Sorting is sorted according to ASCII.
Import Java.util.iterator;import Java.util.treeset;public class Treesetdemo {public static void main (string[] args) {//T ODO auto-generated Method Stubmethod1 ();} public static void Method1 () {TreeSet ts = new TreeSet () Ts.add ("CBA"), Ts.add ("AAA"), Ts.add ("BCA"), Ts.add ("ABCD"); Terator it = Ts.iterator (); while (It.hasnext ()) {System.out.println (It.next ());} /** * Print Result: AAA ABCD BCA CBA treeset elements are ordered in alphabetical order by ASCII code */}}
Custom class student, with attributes name and age, sorted by age from small to large.
Import Java.util.iterator;import java.util.treeset;class Student {private String name;private int age;public Student ( String name, int age) {super (); this.name = Name;this.age = age;} Set and get Methodspublic String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}} public class Treesetdemo {public static void main (string[] args) {//TODO auto-generated method Stubtreeset ts = new TreeS ET (); Ts.add (New Student ("9"), Ts.add (New Student (""), Ts.add (New Student (""); Ts.add (New Student ("04" ); Iterator it = Ts.iterator (); while (It.hasnext ()) {Student Student = (Student) it.next (); System.out.println (Student.getname () + "::::" +student.getage ());}}}
An error occurred while running the discovery:
Exception in thread "main" Java.lang.ClassCastException:Student cannot is cast to Java.lang.Comparableat Java.util.TreeM Ap.compare (treemap.java:1290) at Java.util.TreeMap.put (treemap.java:538) at Java.util.TreeSet.add (treeset.java:255 ) at Treesetdemo.main (treesetdemo.java:39)
Because TreeSet is an ordered set, it needs to know how the elements inside it are arranged, so the student class is required to implement the comparable interface .
The comparable interface forcibly implements its overall ordering of objects for each class, which is called natural ordering. There is only one method Comparetoxuyao replication in this interface.
Import Java.util.iterator;import Java.util.treeset;class Student implements comparable{private String name;private int Age;public Student (String name, int age) {super (); this.name = Name;this.age = age;} Set and get Methodspublic String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} public int compareTo (Object o) {if (!) ( o instanceof Student)) throw new RuntimeException ("Not student object"); Student Student = (Student) o;if (This.age > Student.age) return 1;if (this.age = = student.age) return 0;return-1;}} public class Treesetdemo {public static void main (string[] args) {//TODO auto-generated method Stubtreeset ts = new TreeS ET (); Ts.add (New Student ("9"), Ts.add (New Student (""), Ts.add (New Student (""); Ts.add (New Student ("04" ); Iterator it = Ts.iterator (); while (It.hasnext ()) {Student Student = (Student) it.next (); System.out.println (Student.getname () + "::::" +student.getage ());}}}
By using this method, you can print out the results:
03::::901::::1002::::1504::::18
However, when adding an element with the same age as the existing element but with a different name, it is not added.
We add an element like this.
Ts.add (New Student ("05", 18));
But it does not show the result. Because in this CompareTo method it is considered that the same age is the same element, for this, when the age of the same time we have to make another judgment, to determine whether their names are the same.
We will modify the CompareTo method as follows:
public int compareTo (Object o) {if (!) ( o instanceof Student)) throw new RuntimeException ("Not student object"); Student Student = (Student) o;if (This.age > Student.age) return 1;if (this.age = = student.age) {return this.name.compar ETo (student.name);} return-1;}
Running again will reveal that the 05 elements are stored in the collection. So how does the TreeSet compare?
We add such a sentence in the CompareTo method:
System.out.println (this.name + "-----compaerto-----" + student.name);
Used to display its running process.
public int compareTo (Object o) {if (!) ( o instanceof Student)) throw new RuntimeException ("Not student object"); Student Student = (Student) o; System.out.println (this.name + "-----compaerto-----" + student.name), if (This.age > Student.age) return 1;if ( This.age = = student.age) {return this.name.compareTo (student.name);} return-1;}
Operation Result:
----- Compaerto-----0103-----Compaerto-----0104-----Compaerto-----0104-----Compaerto-----0205-----Compaerto-----0105-----COMPAE RTo-----0205-----Compaerto-----0403::::901::::1002::::1504::::1805::::18
With this result, we can see how this program works:
First, 01 deposit first
02 and 01 are compared and deposited
03 compared to 01, deposit
04 and 01 02 Comparison deposit
05 and 01 02 04 Compare Deposit
Why is it like this? Why isn't it compared to all the elements before this element? It is because the underlying data structure of TreeSet is a two-fork tree.
We draw a picture according to this procedure, when the first element is deposited, a node 10 (age) is created for this element, and thereafter the element is compared to the 10 node at the time of deposit, which is smaller than he creates a node on the left side of it, which creates nodes on the right side of it, and so on, such as 04 deposit, Compare with 10, compared to 10, should be stored to the right, but the right side of the element 15, and then 15 compared, than 15, on the right side of 15 to create a node, deposit.
Java---TreeSet of---set set