1. Knowledge Point Understanding
Comparator and comparable are used to implement the comparison and ordering of elements in the collection, so they are often defined outside the collection Comparator The method of the interface and the implementation of the comparable interface within the set of methods to implement sorting
Same point:
The comparison between the two methods comparable compareTo and compare returned results with three negative numbers, 0, positive numbers, The relationship represented is less than, equal to, greater than
Different points:
Comparator is located under the java.util package, belonging to A member of the Collection;comparable is located Java.lang under the bag
Comparable is an internal comparator, and the object that implements the interface is equivalent to having the ability to sort;theComparator is an external comparer that implements two objects that do not have a sort of implementation The Comparator interface is implemented for sorting, internally and externally, relative to whether the sort code is implemented in the sorted object.
the methods in the Comparator interface are as follows:
the methods in comparable are as follows:
It is recommended that Comparator implement java.io.Serializable interfaces to serialize because they are often serialized after the TreeSet ,TreeMap and other data structures; theequals method is often overridden to compare whether the referenced object is the same;
2. Practical Application
Implemented according to the student's age from small to large, if the age is equal, then by the name of the dictionary sort
2.1throughComparatorexternal comparators to implement sorting
Student class:
1 Public classStudent {2 PrivateString name;3 PrivateInteger age;4 PublicString GetName () {5 returnname;6 }7 Public voidsetName (String name) {8 This. Name =name;9 }Ten Student (String name, Integer age) { One This. name=name; A This. age=Age ; - } - the PublicInteger getage () { - returnAge ; - } - + Public voidsetage (Integer age) { - This. Age =Age ; + } A at @Override - PublicString toString () { - return"Student{" + -"Name=" + name + ' \ ' + -", age=" + Age + -‘}‘; in}
Comparator Implementing Sort classes:
1 /**2 * Implemented according to the student's age from small to large, if the age is equal, then by the name of the dictionary sort3 */4 Public classStudentcompartorImplementsComparator<student> {5 6 @Override7 Public intCompare (Student s1, Student S2) {8 return(S1.getage ()-s2.getage () ==0)? (S1.getname (). CompareTo (S2.getname ())):(S1.getage ()-s2.getage ());9 }Ten Public Static voidMain (string[] args) { OneStudentcompartor PC =Newstudentcompartor (); AStudent p1=NewStudent ("Zhangsan", 20); -Student p2=NewStudent ("Lisi", 25); -Student p3=NewStudent ("Zhangsan", 25); the if(Pc.compare (P1,P2) <0){ - System.out.println (p1); - System.out.println (p2); -}Else{ + System.out.println (p2); - System.out.println (p1); + } A //the two are of equal age, sorted by name at if(Pc.compare (P2,P3) <0){ - System.out.println (p2); - System.out.println (p3); -}Else{ - System.out.println (p3); - System.out.println (p2); in } - } to}
Operation Result:
2.2throughcomparableinternal comparators to implement sorting
Student class: Implements the comparable interface
1 Public classStudentImplementsComparable<student> {2 PrivateString name;3 PrivateInteger age;4 5 PublicString GetName () {6 returnname;7 }8 9 Public voidsetName (String name) {Ten This. Name =name; One } A - PublicInteger getage () { - returnAge ; the } - - Public voidsetage (Integer age) { - This. Age =Age ; + } - + Student (String name, Integer age) { A This. Name =name; at This. Age =Age ; - } - - @Override - Public intcompareTo (Student s2) { - return( This. Getage ()-s2.getage () = = 0)? ( This. GetName (). CompareTo (S2.getname ())) in: ( This. Getage ()-s2.getage ()); - } to + @Override - PublicString toString () { the return"student{" + "name=" + name + ' \ ' + ", age=" + Age + '} '; * } $}
ordering of the internal comparator comparable implementation:
1 /**2 * Implemented according to the student's age from small to large, if the age is equal, then by the name of the dictionary sort3 */4 Public classstudentcomparable {5 Public Static voidMain (string[] args) {6Student P1 =NewStudent ("Zhangsan", 20);7Student P2 =NewStudent ("Lisi", 25);8Student P3 =NewStudent ("Zhangsan", 25);9 if(P1.compareto (P2) < 0) {Ten System.out.println (p1); One System.out.println (p2); A}Else { - System.out.println (p2); - System.out.println (p1); the } - //the two are of equal age, sorted by name - if(P2.compareto (P3) < 0) { - System.out.println (p2); + System.out.println (p3); -}Else { + System.out.println (p3); A System.out.println (p2); at } - } -}
The result of the operation is shown above.
The connection and difference between comparator and comparable