comparable和comparator
Comparable
Comparable可以認(rèn)為是一個(gè)內(nèi)部比較器,實(shí)現(xiàn)了Comparable接口的類有一個(gè)特點(diǎn),就是這些類是可以和自己比較的,在compareTo方法中指定具體的比較方法。
compareTo方法的返回值是int,有三種情況:
1、比較者大于被比較者(也就是compareTo方法里面的對(duì)象),那么返回正整數(shù)
2、比較者等于被比較者,那么返回0
3、比較者小于被比較者,那么返回負(fù)整數(shù)
舉例:定義Student類,實(shí)現(xiàn)Comparable接口,重寫compareTo方法,比較兩者的age。
public class Student implements Comparable<Student>{
private String name;
private String age;
public Student() {
}
public Student(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public int compareTo(Student o) {
return this.age.compareTo(o.age);
}
}
public class Test {
public static void main(String[] args) {
Student student1 = new Student("zs","11");
Student student2 = new Student("ls","12");
Student student3 = new Student("ww","10");
System.out.println("age 11 與 age 12 比較====" + student1.compareTo(student2));
System.out.println("age 11 與 age 10 比較====" + student1.compareTo(student3));
System.out.println("age 11 與 age 11 比較====" + student1.compareTo(student1));
}
}
結(jié)果:
age 11 與 age 12 比較====-1 age 11 與 age 10 比較====1 age 11 與 age 11 比較====0
Comparator
Comparator可以認(rèn)為是是一個(gè)外部比較器,個(gè)人認(rèn)為有兩種情況可以使用實(shí)現(xiàn)Comparator接口的方式:
1、一個(gè)對(duì)象不支持自己和自己比較(沒有實(shí)現(xiàn)Comparable接口),但是又想對(duì)兩個(gè)對(duì)象進(jìn)行比較
2、一個(gè)對(duì)象實(shí)現(xiàn)了Comparable接口,但是開發(fā)者認(rèn)為compareTo方法中的比較方式并不是自己想要的那種比較方式
Comparator接口里面有一個(gè)compare方法,方法有兩個(gè)參數(shù)T o1和T o2,是泛型的表示方式,分別表示待比較的兩個(gè)對(duì)象,方法返回值和Comparable接口一樣是int,有三種情況:
1、o1大于o2,返回正整數(shù)
2、o1等于o2,返回0
3、o1小于o3,返回負(fù)整數(shù)
舉例:自定義一個(gè)外部比較器StudentComparator,實(shí)現(xiàn)Comparator接口,重寫compare方法,比較兩者的age。
public class StudentComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o1.getAge().compareTo(o2.getAge());
}
}
public class Test {
public static void main(String[] args) {
Student student1 = new Student("zs","11");
Student student2 = new Student("ls","12");
Student student3 = new Student("ww","10");
StudentComparator studentComparator = new StudentComparator();
int compare1 = studentComparator.compare(student1, student2);
int compare2 = studentComparator.compare(student1, student3);
int compare3 = studentComparator.compare(student1, student1);
System.out.println("age 11 與 age 12 比較======" + compare1);
System.out.println("age 11 與 age 10 比較======" + compare2);
System.out.println("age 11 與 age 11 比較======" + compare3);
}
}
結(jié)果:
age 11 與 age 12 比較======-1 age 11 與 age 10 比較======1 age 11 與 age 11 比較======0
總結(jié)
總結(jié)一下,兩種比較器Comparable和Comparator,后者相比前者有如下優(yōu)點(diǎn):
1、如果實(shí)現(xiàn)類沒有實(shí)現(xiàn)Comparable接口,又想對(duì)兩個(gè)類進(jìn)行比較(或者實(shí)現(xiàn)類實(shí)現(xiàn)了Comparable接口,但是對(duì)compareTo方法內(nèi)的比較算法不滿意),那么可以實(shí)現(xiàn)Comparator接口,自定義一個(gè)比較器,寫比較算法
2、實(shí)現(xiàn)Comparable接口的方式比實(shí)現(xiàn)Comparator接口的耦合性要強(qiáng)一些,如果要修改比較算法,要修改Comparable接口的實(shí)現(xiàn)類,而實(shí)現(xiàn)Comparator的類是在外部進(jìn)行比較的,不需要對(duì)實(shí)現(xiàn)類有任何修改。從這個(gè)角度說,其實(shí)有些不太好,尤其在我們將實(shí)現(xiàn)類的.class文件打成一個(gè).jar文件提供給開發(fā)者使用的時(shí)候。實(shí)際上實(shí)現(xiàn)Comparator接口的方式后面會(huì)寫到就是一種典型的策略模式。
當(dāng)然,這不是鼓勵(lì)用Comparator,意思是開發(fā)者還是要在具體場(chǎng)景下選擇最合適的那種比較器而已。
參考資料:
https://www.cnblogs.com/xrq730/p/4850140.html
總結(jié)
以上是生活随笔為你收集整理的comparable和comparator的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 刨根究底字符编码之六——简体汉字编码中区
- 下一篇: 数据库防火墙如何防范SQL注入行为