The comparison principle of determining whether elements are identical in different data structures arraylist equalshashset hashcode equalstreeset the return 0 of the compare of the custom comparator or the CompareTo of the element itself rutren 0treeset look at the following example Class treesetdemo3 {public static void main (String[] args) { Treeset ts = new treeset (New studentcomparatorbyname ());//Comparator //treeset ts = new treeset ();//element itself Ts.add (new Student ("Lisi0" ), Ts.add (New student ("Lisixx")), Ts.add (New student ("Lisi9")), Ts.add (New student (" Lisi8, Ts.add (new student ("Lisixx"), Ts.add (New student ("Lisi4");//ts.add (new Student (Ts.add)); (New student ("Lisi7", 27)); SYSTEM.OUT.PRINTLN (TS);}} Class studentcomparatorbyname implements comparator{public int compare (Object o1 , Object o2) {student s1 = (Student) O1; Student s2 = (Student) O2;int num = s1.getname (). CompareTo (S2.getname ()); return num==0?new integer (S1.getage ()). CompareTo (New integer (S2.getage ())): num;}} Students of the same age are regarded as the same student. Sort by the age of the students. class student implements comparable{private int age;private string name; Student (string name,int age) {this.age = age;this.name = name;} Public int compareto (object obj) {student stu = (Student) obj;int num = new integer (this.age). CompareTo (New integer (stu.age)); Return num==0?this.name.compareto ( Stu.name): Num;/*if (this.age>stu.age) return 1;if (this.age==stu.age) Return this.name.compareto ( Stu.name); Return -1;*//**/}public int getage () {return age;} Public string getname () {return name;} Public string tostring () {return name+ "::" +age;}}
How the different data structures compare