Java中Comparable接口和Comparator接口的使用比較
在Java中,我們會(huì)經(jīng)常使用到自定義類(lèi),那我們?nèi)绾芜M(jìn)行自定義類(lèi)的比較呢?
1.Comparable接口
普通數(shù)據(jù)的比較
int a=10;
int b=91;
System.out.println(a<b);那自定義類(lèi)型可不可以這樣比較呢?看一下代碼

我們發(fā)現(xiàn)會(huì)報(bào)錯(cuò),因?yàn)樽远x類(lèi)型,stu1和stu2里面存的是引用,是無(wú)法直接根據(jù)姓名或年齡進(jìn)行比較的。
1.1Comparable接口的使用
如果想要自定義類(lèi)型根據(jù)年齡和名字進(jìn)行比較,這時(shí)候就要用到我們的Comparable接口。

當(dāng)我們觀察Comparable接口的底層細(xì)節(jié)會(huì)發(fā)現(xiàn)有一個(gè)<T>和一個(gè)方法,<T>代表我們要比較的類(lèi)型,方法是我們根據(jù)實(shí)際情況來(lái)重寫(xiě)compareTo方法,也就是比較的規(guī)則。
1.根據(jù)年齡比較
自定義類(lèi)中具體實(shí)現(xiàn)
class Student implements Comparable<Student>{
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Student o) {
//根據(jù)年齡比較
/*if(this.age>o.age){
return 1;
}else if (this.age==o.age){
return 0;
}else {
return -1;
}*/
return this.age-o.age;
}
}完整代碼
class Student implements Comparable<Student>{
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Student o) {
//根據(jù)年齡比較
/*if(this.age>o.age){
return 1;
}else if (this.age==o.age){
return 0;
}else {
return -1;
}*/
return this.age-o.age;
}
}
public class Test {
public static void main(String[] args) {
Student stu1=new Student("zhansan",18);
Student stu2=new Student("man",24);
System.out.println(stu1.compareTo(stu2));
}
}
2.根據(jù)名字比較
class Student implements Comparable<Student>{
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Student o) {
return this.name.compareTo(o.name);
}
}
public class Test {
public static void main(String[] args) {
Student stu1=new Student("zhansan",18);
Student stu2=new Student("man",24);
System.out.println(stu1.compareTo(stu2));
}
}
由于名字是String類(lèi),String類(lèi)在底層中也實(shí)現(xiàn)了compareTo方法,所以我們可以直接調(diào)用compareTo方法來(lái)實(shí)現(xiàn)名字的比較。
3. 多個(gè)對(duì)象之間的比較
多個(gè)對(duì)象我們可以用一個(gè)對(duì)應(yīng)類(lèi)的數(shù)組來(lái)存儲(chǔ),然后思路就是讓數(shù)組里面的元素就行比較。
這里模擬了冒泡排序進(jìn)行比較。
根據(jù)名字來(lái)排序
import java.util.Arrays;
class Student implements Comparable<Student>{
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student o) {
return this.name.compareTo(o.name);
}
}
public class Test {
public static void mysort(Comparable[] comparables){
for (int i = 0; i < comparables.length-1; i++) {
for(int j=0;j<comparables.length-1-i;j++){
if(comparables[j].compareTo(comparables[j+1])>0){
Comparable tmp=comparables[j];
comparables[j]=comparables[j+1];
comparables[j+1]=tmp;
}
}
}
}
public static void main(String[] args) {
Student[] students=new Student[]{
new Student("zhansan",18),
new Student("man",24),
new Student("lebron",23)
};
mysort(students);
System.out.println(Arrays.toString(students));
}
}

根據(jù)年齡來(lái)排序
import java.util.Arrays;
class Student implements Comparable<Student>{
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student o) {
return this.age-o.age;
}
}
public class Test {
public static void mysort(Comparable[] comparables){
for (int i = 0; i < comparables.length-1; i++) {
for(int j=0;j<comparables.length-1-i;j++){
if(comparables[j].compareTo(comparables[j+1])>0){
Comparable tmp=comparables[j];
comparables[j]=comparables[j+1];
comparables[j+1]=tmp;
}
}
}
}
public static void main(String[] args) {
Student[] students=new Student[]{
new Student("zhansan",18),
new Student("man",24),
new Student("lebron",23)
};
mysort(students);
System.out.println(Arrays.toString(students));
}
}

4.總結(jié)
1.當(dāng)前階段如果我們想要進(jìn)行自定義類(lèi)型之間的比較,我們要使用Comparable接口。
2.重寫(xiě)接口里面的方法是我們根據(jù)需求來(lái)決定如何重寫(xiě)compareTo方法,重寫(xiě)后的compareTo方法里面的具體實(shí)現(xiàn)就是我們的比較規(guī)則。
2.Comparator接口
我們發(fā)現(xiàn)當(dāng)我們使用Comparable接口時(shí)并不是那么靈活,因?yàn)樗鼘?shí)現(xiàn)的比較規(guī)則是寫(xiě)死的,如果我們想要換一種比較規(guī)則,我們必須要對(duì)實(shí)現(xiàn)對(duì)比較方法里面的重新構(gòu)造。
那有沒(méi)有比較靈活的比較方式呢?答案就是Comparator接口。
AgeComparator類(lèi)
public class AgeComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.age- o2.age;
}
}
NameComparator類(lèi)
public class NameComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.name.compareTo(o2.name);
}
}
主函數(shù)部分
根據(jù)年齡排序
public class Test {
public static void main(String[] args) {
Student[] students=new Student[]{
new Student("zhansan",18),
new Student("man",24),
new Student("lebron",23)
};
NameComparator nameComparator=new NameComparator();
AgeComparator ageComparator=new AgeComparator();
Arrays.sort(students,ageComparator);
System.out.println(Arrays.toString(students));
}
}

根據(jù)名字比較
public class Test {
public static void main(String[] args) {
Student[] students=new Student[]{
new Student("zhansan",18),
new Student("man",24),
new Student("lebron",23)
};
NameComparator nameComparator=new NameComparator();
AgeComparator ageComparator=new AgeComparator();
Arrays.sort(students,nameComparator);
System.out.println(Arrays.toString(students));
}
}

這里我們定義了AgeComparator類(lèi)和NameComparator類(lèi),它們都使用了Comparator這個(gè)接口,
然后在自己的類(lèi)里面重寫(xiě)了compareTo方法。
根據(jù)以上類(lèi)實(shí)現(xiàn)的對(duì)象可以認(rèn)為是比較規(guī)則,將這些對(duì)象作為sort函數(shù)的參數(shù),就可以靈活實(shí)現(xiàn)不同比較方式的轉(zhuǎn)變。
相對(duì)于Comparable接口來(lái)說(shuō),Comparator不需要改變函數(shù)內(nèi)部的具體實(shí)現(xiàn)來(lái)改變比較規(guī)則,只需改變函數(shù)的參數(shù)就行了,這樣更安全也更方便。
總結(jié)
到此這篇關(guān)于Java中Comparable接口和Comparator接口使用的文章就介紹到這了,更多相關(guān)Java Comparable和Comparator接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java根據(jù)模板導(dǎo)出Excel報(bào)表并復(fù)制模板生成多個(gè)Sheet頁(yè)
本文主要介紹了Java根據(jù)模板導(dǎo)出Excel報(bào)表并復(fù)制模板生成多個(gè)Sheet頁(yè)的方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
Eclipse Debug模式的開(kāi)啟與關(guān)閉問(wèn)題簡(jiǎn)析
這篇文章主要介紹了Eclipse Debug模式的開(kāi)啟與關(guān)閉問(wèn)題簡(jiǎn)析,同時(shí)向大家介紹了一個(gè)簡(jiǎn)單的debug模式啟動(dòng)不起來(lái)的解決方法,希望對(duì)大家有所幫助。2017-10-10
自定義注解SpringBoot防重復(fù)提交AOP方法詳解
該文章描述了一個(gè)防止重復(fù)提交的流程,通過(guò)HttpServletRequest對(duì)象獲取請(qǐng)求信息,生成唯一標(biāo)識(shí),使用Redis分布式鎖判斷請(qǐng)求是否重復(fù)提交,并通過(guò)自定義注解和切面實(shí)現(xiàn)2025-12-12
JAVA中l(wèi)ist,set,數(shù)組之間的轉(zhuǎn)換詳解
以下是對(duì)JAVA中l(wèi)ist,set,數(shù)組之間的轉(zhuǎn)換進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-09-09
Java使用自定義注解實(shí)現(xiàn)為事件源綁定事件監(jiān)聽(tīng)器操作示例
這篇文章主要介紹了Java使用自定義注解實(shí)現(xiàn)為事件源綁定事件監(jiān)聽(tīng)器操作,結(jié)合實(shí)例形式分析了java自定義注解、注解處理、事件監(jiān)聽(tīng)與響應(yīng)等相關(guān)操作技巧,需要的朋友可以參考下2019-10-10
vue用CryptoJS加密,java用CryptoUtil解密
CryptoJS是一個(gè)JavaScript庫(kù),提供了一系列密碼學(xué)函數(shù)和工具,用于加密、解密、生成摘要等任務(wù),本文vue前端使用CryptoJS加密,java后端使用CryptoUtil解密2024-09-09
SpringBoot?自定義注解實(shí)現(xiàn)涉密字段脫敏
關(guān)于數(shù)據(jù)脫敏,網(wǎng)上的文章都是硬編碼規(guī)則,比如對(duì)身份證,手機(jī)號(hào),郵件地址等固定寫(xiě)法脫敏。本文在此基礎(chǔ)上,拓展動(dòng)態(tài)從數(shù)據(jù)庫(kù)查出涉密關(guān)鍵字執(zhí)行脫敏操作。感興趣的同學(xué)可以參考閱讀2023-03-03

