1, comparable introduction
comparable is a sort interface, if a class implements the interface, the class itself can be sorted . Note that in addition to the basic data type (eight basic data types) array or list, the remaining types of objects, Collections.sort or Arrays.sort It is not supported to sort directly, because the object itself is not "sequential", unless you implement the comparable interface or customize the comparable object, you can sort by specifying a collation.
comparable Source is a method,
1 Public Interface Comparable<t> {2public int compareTo (T o); 3 }
Generic T represents the type of object to be compared, CompareTo compares the size of the value between objects, and if the object is less than, equal to, or greater than the specified object, returns a negative integer, 0, or a positive integer, respectively.
Define an object:
1 Public classPersonImplementsComparable<person>{2 Public intAge ;3 4 PublicPerson (intAge ) {5 This. Age =Age ;6 }7 PublicString toString () {8 return"{" +9"Age=" + Age +Ten‘}‘; One } A @Override - Public intcompareTo (Person o) { - //sort between person objects by name the return This. Age-O.age; - } -}
Sequencing tests:
Public Static voidMain (string[] args) {person[] PS=Newperson[]{NewPerson (1),NewPerson (4),NewPerson (2),NewPerson (7),NewPerson (9),NewPerson (8),NewPerson (3),NewPerson (0),NewPerson (1)}; System.out.println ("Before sorting:" +arrays.tostring (PS)); //to sortArrays.sort (PS); System.out.println ("After sorting:" +arrays.tostring (PS)); }
Before ordering: [{age=1}, {age=4}, {age=2}, {age=7}, {age=9}, {age=8}, {age=3}, {age=0}, {age=1}] After sorting: [{age =0}, {age=1}, { Age=1}, {age=2}, {age=3}, {age=4}, {age=7}, {age=8}, {age=9}]
2, Comparator introduction
If a class itself does not implement the comparable interface, we want to sort him out by customizing the Comparator comparator for comparison. The basis of the custom sort in this comparator.
comparator source of the main two interface methods:
1 Public Interface Comparator<t>2 {3 int compare (t O1, T O2); 4 Boolean equals (Object obj); 5 }
Compare is the primary method that must be implemented, and the Equals method can not be implemented. Returns the comparison result in compare if the object is less than, equal to, or greater than the specified object, and returns a negative integer, 0, or a positive integer, respectively.
Define a class to sort and implement the comparable interface for the class :
1 Private Static classman{2 Public intAge ;3 PublicMans (intAge ) {4 This. Age =Age ;5 }6 PublicString toString () {7 return"{" +8"Age=" + Age +9‘}‘;Ten } One}
sort:
1 @Test2 Public voidtest_1 () {3Man[] PS =Newman[]{NewMan (1),NewMan (4),NewMans (2),4 NewMan (7),NewMan (9),NewMan (8),NewMan (3),NewMan (0),NewMans (1)};5 //Array goto list6Arraylist<man> ap =NewArraylist<man>(Arrays.aslist (PS));7 8System.out.println ("Before sorting:" +AP);9 //Custom SequencerTenCollections.sort (AP,NewComparator<man>() { One @Override A Public intCompare (man O1, man O2) { - //Sort by age - returnO1.age-O2.age; the } - }); - -System.out.println ("After sorting:" +AP); +}
Before ordering: [{age=1}, {age=4}, {age=2}, {age=7}, {age=9}, {age=8}, {age=3}, {age=0}, {age=1}] After sorting: [{age =0}, {age=1}, { Age=1}, {age=2}, {age=3}, {age=4}, {age=7}, {age=8}, {age=9}]
3. Summary comparison
Comparable defines the collation within the class, Comparator defines the collation externally, comparable the equivalent of an "internal sequencer", Comparator equivalent to an "external sequencer", which is defined once, The latter can be in the case of non-modification of the source of the order, strengths.
Comparable and comparator comparisons in Java