Java中Comparable與Comparator的區(qū)別解析
Comparable與Comparator的區(qū)別
1.理論知識
Comparable
實現(xiàn)Comparable接口,重寫compareTo方法。一般在實體類定義的時候就可以選擇實現(xiàn)該接口,提供一個默認的排序方式,供Arrays.sort和Collections.sort使用,只有一種排序方式,很難滿足復雜的排序要求。
compareTo方法的返回值是int,有三種情況:
a、當前對象大于被比較對象(compareTo方法里面的形參),返回正整數(shù)
b、當前對象等于被比較對象,返回0
c、當前對象小于被比較對象,返回負整數(shù)
Comparator
實現(xiàn)Comparator接口,重寫compare方法,一般在實體類中未定義排序方法或?qū)嶓w類中的排序方法不滿足需求的情況下來實現(xiàn)該接口。實現(xiàn)方法可以是寫一個比較器類,也可是匿名內(nèi)部類。實現(xiàn)方式靈活,可以提供多種排序選擇。
Ccompare方法有兩個參數(shù)o1和o2,分別表示待比較的兩個對象,方法返回值也是int,有三種情況:
a、o1大于o2,返回正整數(shù)
b、o1等于o2,返回0
c、o1小于o2,返回負整數(shù)
2.代碼驗證
上代碼
實體類Student,實現(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; } }
測試類
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(); } }
- 使用默認排序(前提是實體類實現(xiàn)Comparable接口,否則會報錯)
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簡化:
Arrays.sort(students, (o1, o2) -> o1.getScore() - o2.getScore());
正序可以再簡化,逆序不行
Arrays.sort(students, Comparator.comparingInt(Student::getScore));
到此這篇關(guān)于Java中Comparable與Comparator的區(qū)別解析的文章就介紹到這了,更多相關(guān)Comparable與Comparator的區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis通用Mapper實現(xiàn)原理及相關(guān)內(nèi)容
今天小編就為大家分享一篇關(guān)于MyBatis通用Mapper實現(xiàn)原理及相關(guān)內(nèi)容,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12使用Spring Boot快速構(gòu)建基于SQLite數(shù)據(jù)源的應用
為了提供一個單包易部署的服務(wù)器應用,考慮使用Spring Boot,因為其集成了Apache Tomcat,易于運行,免去絕大部分了服務(wù)器配置的步驟2017-08-08Java8中Optional類型和Kotlin中可空類型的使用對比
這篇文章主要給大家介紹了關(guān)于Java8中Optional類型和Kotlin中可空類型的使用對比,文中通過示例代碼給大家介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-09-09