比較器:Compare接口與Comparator接口區別與理解


一、實現Compare接口與Comparator接口的類,都是為了對象實例數組排序的方便,因為可以直接調用

 java.util.Arrays.sort(對象數組名稱),可以自定義排序規則。

不同之處:

 1 排序規則實現的方法不同

    Comparable接口的方法:compareTo(Object o)

    Comparator接口的方法:compare(T o1, To2)

 2 類設計前后不同

      Comparable接口用於在類的設計中使用;

   Comparator接口用於類設計已經完成,還想排序(Arrays);

二、Comparable接口的實例操作

 Student類創建時實現Comparable接口,覆寫compareTo()方法,

成績按從高到低排序,成績相等按年齡從小到大排序。

package ch11.lei.ji;
/*實現Comparator接口的類可以方便的排序,
 * 覆寫compareTo接口
 * java.util.Arrays.sort(對象類數組),*/
class Student implements Comparable<Student> {    // 指定類型為Student
    private String name ;
    private int age ;
    private float score ;
    public Student(String name,int age,float score){
        this.name = name ;
        this.age = age ;
        this.score = score ;
    }
    public String toString(){
        return name + "\t\t" + this.age + "\t\t" + this.score ;
    }
    public int compareTo(Student stu){    // 覆寫compareTo()方法,實現排序規則的應用
        if(this.score>stu.score){
            return -1 ;
        }else if(this.score<stu.score){
            return 1 ;
        }else{
            if(this.age>stu.age){
                return 1 ;
            }else if(this.age<stu.age){
                return -1 ;
            }else{
                return 0 ;
            }
        }    
    }
};
public class Comparable01{
    public static void main(String args[]){
        Student stu[] = {new Student("張三",20,90.0f),
            new Student("李四",22,90.0f),new Student("王五",20,99.0f),
            new Student("趙六",20,70.0f),new Student("孫七",22,100.0f)} ;
        java.util.Arrays.sort(stu) ;    // 進行排序操作
        for(int i=0;i<stu.length;i++){    // 循環輸出數組中的內容
            System.out.println(stu[i]) ;
        }
    }
};

三、Comparator接口實例操作

Student01類原先沒有比較器,類完成后構建一個比較器StudentComparator類

按年齡從大到小排序。

package ch11.lei.ji;
import java.util.* ;
 class Student01{    // 指定類型為Student
    private String name ;
    private int age ;
    public Student01(String name,int age){
        this.name = name ;
        this.age = age ;
    }
    public boolean equals(Object obj){    // 覆寫equals方法
        if(this==obj){
            return true ;
        }
        if(!(obj instanceof Student)){
            return false ;
        }
        Student01 stu = (Student01) obj ;
        if(stu.name.equals(this.name)&&stu.age==this.age){
            return true ;
        }else{
            return false ;
        }
    }
    public void setName(String name){
        this.name = name ;
    }
    public void setAge(int age){
        this.age = age ;
    }
    public String getName(){
        return this.name ;
    }
    public int getAge(){
        return this.age ;
    }
    public String toString(){
        return name + "\t\t" + this.age  ;
    }
};

class StudentComparator implements Comparator<Student01>{    // 實現比較器
    // 因為Object類中本身已經有了equals()方法
    public int compare(Student01 s1,Student01 s2){
        if(s1.equals(s2)){
            return 0 ;
        }else if(s1.getAge()<s2.getAge()){    // 按年齡比較
            return 1 ;
        }else{
            return -1 ;
        }
    }
};

public class Comparator01{
    public static void main(String args[]){
        Student01 stu[] = {new Student01("張三",20),
            new Student01("李四",22),new Student01("王五",20),
            new Student01("趙六",20),new Student01("孫七",22)} ;
        java.util.Arrays.sort(stu,new StudentComparator()) ;    // 進行排序操作
        for(int i=0;i<stu.length;i++){    // 循環輸出數組中的內容
            System.out.println(stu[i]) ;
        }
    }
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM