Java中Comparable與Comparator的區(qū)別解析
Comparable與Comparator的區(qū)別
1.理論知識(shí)
Comparable
實(shí)現(xiàn)Comparable接口,重寫compareTo方法。一般在實(shí)體類定義的時(shí)候就可以選擇實(shí)現(xiàn)該接口,提供一個(gè)默認(rèn)的排序方式,供Arrays.sort和Collections.sort使用,只有一種排序方式,很難滿足復(fù)雜的排序要求。
compareTo方法的返回值是int,有三種情況:
a、當(dāng)前對(duì)象大于被比較對(duì)象(compareTo方法里面的形參),返回正整數(shù)
b、當(dāng)前對(duì)象等于被比較對(duì)象,返回0
c、當(dāng)前對(duì)象小于被比較對(duì)象,返回負(fù)整數(shù)
Comparator
實(shí)現(xiàn)Comparator接口,重寫compare方法,一般在實(shí)體類中未定義排序方法或?qū)嶓w類中的排序方法不滿足需求的情況下來實(shí)現(xiàn)該接口。實(shí)現(xiàn)方法可以是寫一個(gè)比較器類,也可是匿名內(nèi)部類。實(shí)現(xiàn)方式靈活,可以提供多種排序選擇。
Ccompare方法有兩個(gè)參數(shù)o1和o2,分別表示待比較的兩個(gè)對(duì)象,方法返回值也是int,有三種情況:
a、o1大于o2,返回正整數(shù)
b、o1等于o2,返回0
c、o1小于o2,返回負(fù)整數(shù)
2.代碼驗(yàn)證
上代碼
實(shí)體類Student,實(shí)現(xiàn)Comparable接口
public class Student implements Comparable<Student> {
private int age;
private String address;
private int score;
public Student(int age, String address, int score) {
this.age = age;
this.address = address;
this.score = score;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
public int getScore() {
return score;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", address='" + address + '\'' +
", score=" + score +
'}';
}
@Override
public int compareTo(Student o) {
// return this.age - o.age;
return o.age - this.age;
}
}測(cè)試類
public class SortTest {
public static void main(String[] args) {
Student s1 = new Student(18, "西安", 98);
Student s2 = new Student(6, "廣州", 68);
Student s3 = new Student(29, "天水", 84);
Student s4 = new Student(18, "廣州", 84);
Student[] students = new Student[4];
students[0] = s1;
students[1] = s2;
students[2] = s3;
students[3] = s4;
Arrays.sort(students);
// Arrays.sort(students,new StudentScoreComparator());
// Arrays.sort(students, new Comparator<Student>() {
// @Override
// public int compare(Student o1, Student o2) {
// return o1.getScore() - o2.getScore();
// }
// });
// Arrays.sort(students, (o1, o2) -> o1.getScore() - o2.getScore());
// Arrays.sort(students, Comparator.comparingInt(Student::getScore));
for (int i = 0; i < students.length; i++) {
System.out.println(students[i]);
}
}
}比較器類
public class StudentScoreComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if (o1.getScore() == o2.getScore()) {
return o1.getAge() - o2.getAge();
}
return o2.getScore() - o1.getScore();
}
}- 使用默認(rèn)排序(前提是實(shí)體類實(shí)現(xiàn)Comparable接口,否則會(huì)報(bào)錯(cuò))
Arrays.sort(students);
compareTo方法:用age字段排序
正序:this.age - o.age;
逆序:o.age - this.age;
- 使用比較器類排序
Arrays.sort(students,new StudentScoreComparator());
compare方法:score逆序,age正序
正序:o1-o2
逆序:o2-o1
匿名內(nèi)部類排序:score正序
Arrays.sort(students, new Comparator() {
@Override
public int compare(Student o1, Student o2) {
return o1.getScore() - o2.getScore();
}
});lambda簡(jiǎn)化:
Arrays.sort(students, (o1, o2) -> o1.getScore() - o2.getScore());
正序可以再簡(jiǎn)化,逆序不行
Arrays.sort(students, Comparator.comparingInt(Student::getScore));
到此這篇關(guān)于Java中Comparable與Comparator的區(qū)別解析的文章就介紹到這了,更多相關(guān)Comparable與Comparator的區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis通用Mapper實(shí)現(xiàn)原理及相關(guān)內(nèi)容
今天小編就為大家分享一篇關(guān)于MyBatis通用Mapper實(shí)現(xiàn)原理及相關(guān)內(nèi)容,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
SpringBoot自定義bean綁定實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot自定義bean綁定,最常見的配置綁定的場(chǎng)景,是在自定義的bean中通過@Value注解將某個(gè)屬性和對(duì)應(yīng)的配置綁定2022-10-10
使用Spring Boot快速構(gòu)建基于SQLite數(shù)據(jù)源的應(yīng)用
為了提供一個(gè)單包易部署的服務(wù)器應(yīng)用,考慮使用Spring Boot,因?yàn)槠浼闪薃pache Tomcat,易于運(yùn)行,免去絕大部分了服務(wù)器配置的步驟2017-08-08
SpringMVC異常處理知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于SpringMVC異常處理相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-10-10
Java8中Optional類型和Kotlin中可空類型的使用對(duì)比
這篇文章主要給大家介紹了關(guān)于Java8中Optional類型和Kotlin中可空類型的使用對(duì)比,文中通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09

