com.sinosig.epassport.application Package; Import java.util.Collections; Import java.util.Comparator; Import Classes in java.util *;. Import java.lang.Comparable; / ** * qwp123 2019/8/25 22:10 * / public class comparableComparator { / ** * @desc "comparator" and "Comparable" in comparison programs. * (01) "the Comparable" * It is a sort interface contains only one function compareTo (). * A class implements the Comparable interface, it means "the class itself supports sorting", which can be sorted directly through Arrays.sort () or Collections.sort (). * (02) "Comparator" * it is a relatively interface comprising two functions: compare () and equals (). * A class implements Comparator interface, then it is a "comparator." Other classes may be sorted according to the comparator. * <P> * A class itself implements the Comparable comparator, it means that it supports sorting itself; if it did not implement Comparable itself, can also be sorted by external comparators Comparator. * / Public static void main (String [] args) { // new ArrayList (dynamic array) ArrayList <the Person> List = new new ArrayList <the Person> (); // add the object to the ArrayList list.add (new Person ( " CCC ", 20 is)); List.add (new new the Person (" the AAA ", 30)); List.add (new new the Person (" BBB ", 10)); List.add (new new the Person (" ddd ", 40) ); // list printing original sequence System.out.printf ( "original Sort, list:% S \ n-", list); // list of sorted // based here "Person implemented Comparable <String> Interface "sort, i.e. based on" sorted name " the Collections.sort (List); System.out.printf ( "the Name Sort, List:% S \ n-", List); // AscAgeComparator is sorted is: The "age" in ascending order of the Collections.sort (List, new new AscAgeComparator ()); the System. form out.printf ( "the Asc (Age) Sort, list:% S \ n-', list); // the" comparator (DescAgeComparator) ", of the sort list // DescAgeComparator is sorted is: the" age "of Sort descending the Collections.sort (List, new new DescAgeComparator ()); System.out.printf ( "Asc (Age) Sort, List:% S \ n-", List); // determines whether the same person two testEquals (); } / ** * @desc two Person Comparative test for equality. * Due to Person implements equals () function: if two person's age, name are equal, are considered equal two person. * So, here's equal p1 and p2. * <P> * TODO: if in Person equals remove () function, p1 is not equal to P2 * / Private static void testEquals () { Person p1 = new new Person ( "Eee", 100); Person P2 = new new Person ( "Eee", 100); IF (p1. the equals (P2)) { System.out.printf ( "EQUAL% S% S \ n-", P1, P2); } the else { System.out.printf ( "the NOT EQUAL% S% S \ n-", P1, P2 ); } } / ** * @desc the Person class. * Person implements Comparable interface, which means that Person itself supports sorting * / Private static class Person the implements Comparable <Person> { int Age; String name; public Person (String name, int Age) { = name this.name; Age = this.age; } public String getName() { return name; } public int getAge () { return Age; } public String toString () { return name + "-" + Age; } / ** * compare two Person equality: If they name and age are equal, they are considered equivalent * / Boolean the equals (the Person Person) { IF (this.age this.name == == person.age && PERSON.NAME) return to true; return to false; } / ** * @desc achieve "Comparable <String>" interface, i.e., rewriting compareTo <T t> function. * Here is a comparison by "person's name" * / @Override public int the compareTo (the Person Person) { name.compareTo return (PERSON.NAME); // return this.name - PERSON.NAME; } } / ** * @desc AscAgeComparator comparator * it "age of ascending comparator Person" * / Private static class AscAgeComparator comparator the implements <the Person> { @Override public int Compare (the Person P1, the Person P2) { return p1.getAge () - p2.getAge (); } } / ** * @desc DescAgeComparator comparator * it "Person's age ascending comparator " * / Private static class DescAgeComparator the implements comparator <the Person> { @Override public int compare(Person p1, Person p2) { return p2.getAge() - p1.getAge(); } } }