Preface : In Java in the data sorting, comparable and comparator must encounter, ordinary string, integer and other types, has implemented the comparable interface, and sometimes, We need to sort some other classes that do not have an intrinsic sort relationship, so we need to implement the above two interfaces, but it will be different.
Before searching for the relationship between the two, see such an article, comparable and comparator, some of the words are a bit too esoteric, but I would like to summarize, the two biggest difference is that if the implementation of comparable, it means that the class collation is fixed, If you implement comparator, the class itself is not collation, but by making an external rule to sort it, you can specify many types of collations .
So let's take a look at an example of what I've done, and we're going to create a new beauty class (Beautygirl) that has the age, height property, and so on the principle of age precedence over height, the way it is implemented.
Comparable
Packagecom.mwq.comparable; Public class beautygirl implements comparable<beautygirl> { Private intAgePrivateDouble height; Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } PublicDoublegetheight() {returnHeight } Public void setheight(Double height) { This. height = height; }@Override Public int CompareTo(Beautygirl Girl) {if( This. getage () = = Girl.getage ()) {returnCompareheight (girl); }Else{return This. Getage ()-girl.getage (); } }Private int Compareheight(Beautygirl Girl) {returnGirl.getheight (). CompareTo ( This. GetHeight ()); }@Override PublicStringtoString() {return "Beauty {age:"+ This. Getage () +"Height:"+ This. GetHeight () +"}"; }}
Implementing the comparable interface means implementing the CompareTo method in the beauty class, which means that the class must be age-first over height when sorting.
Take a look at the test class
Packagecom.mwq.comparable;ImportJava.util.ArrayList;ImportJava.util.Collections;ImportJava.util.List; Public class Test { Public Static void Main(string[] args) {List<beautygirl> girls =NewArraylist<beautygirl> (); Beautygirl girl =NewBeautygirl (); Girl.setage ( -); Girl.setheight (170.00); Girls.add (girl); Girl =NewBeautygirl (); Girl.setage ( -); Girl.setheight (183.00); Girls.add (girl); Girl =NewBeautygirl (); Girl.setage ( -); Girl.setheight (180.30); Girls.add (girl); Girl =NewBeautygirl (); Girl.setage ( +); Girl.setheight (170.00); Girls.add (girl); Girl =NewBeautygirl (); Girl.setage ( +); Girl.setheight (170.00); Girls.add (girl); for(Beautygirl beautygirl:girls) {System.out.println (beautygirl); } collections.sort (girls); System.out.println ("Sort---------------------------------------------"); for(Beautygirl beautygirl:girls) {System.out.println (beautygirl); } }}
The output results are as follows :
美女{年龄:28身高:170.0}美女{年龄:25身高:183.0}美女{年龄:28身高:180.3}美女{年龄:32身高:170.0}美女{年龄:21身高:170.0}排序一下---------------------------------------------美女{年龄:21身高:170.0}美女{年龄:25身高:183.0}美女{年龄:28身高:170.0}美女{年龄:28身高:180.3}美女{年龄:32身高:170.0}
Comparator
PackageCom.mwq.comparator; Public class beautygirl { Private intAgePrivateDouble height; Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } PublicDoublegetheight() {returnHeight } Public void setheight(Double height) { This. height = height; }@Override PublicStringtoString() {return "Beauty {age:"+ This. Getage () +"Height:"+ This. GetHeight () +"}"; }}
PackageCom.mwq.comparator;ImportJava.util.Comparator;/** * Achieve the beauty of the sort * * Public class beautygirlcomparator implements Comparator<beautygirl > { @Override Public int Compare(Beautygirl girl1, Beautygirl girl2) {//Put your age first if(Girl1.getage () = = Girl2.getage ()) {returnCompareheight (GIRL1, GIRL2); }Else{returnGirl1.getage ()-girl2.getage (); } }Private int Compareheight(Beautygirl girl1, Beautygirl girl2) {//Put your height in the second place returnGirl1.getheight (). CompareTo (Girl2.getheight ()); }}
new BeautyGirlComparator());
This is used when sorting, as long as you specify a new rule, it will be sorted according to the new rules, without the need to change the Beautygirl class. For example, you can sort by height preference and age.
Summary : The difference between the two lies in the formulation of the collation, if the collation of fickle, then use comparator is a good way, if the rules are fixed, to achieve comparable better, at least write a class less.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java:comparable and Comparator