1つのComparableとComparatorの2つのインタフェースの使用例

7733 ワード

ComparableとComparatorの2つのインタフェースの使用
  Comparable:int compareTo(Object o) ;
  Comparator: int compare(T a, T b) ;
       idea          ,   T     
                ,    Collections    sort  :
    sort(List list):
            compareTo()  ,         Comparable  ,   compareTo()  
    sort(List list,Comparator cp):
      cp            ;

コードは次のとおりです.Studioクラスを定義します.
public class Student /*implements Comparable */{//     Comparable    ,   Comparable
    private String name;
    private int age;
    private float sorce;
/*
    @Override
    public int compareTo(Object o) {
        Student stu=(Student)o;
        int m=(int)((((Student) o).sorce-sorce)*100);//       
        m = m == 0 ? (age - ((Student) o).age) : m;//    ,                    
        return m;
    }
*/

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Float.compare(student.sorce, sorce) == 0 &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, age, sorce);
    }

    public Student() {
    }

    public Student(String name, int age, float sorce) {
        this.name = name;
        this.age = age;
        this.sorce = sorce;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getSorce() {
        return sorce;
    }

    public void setSorce(float sorce) {
        this.sorce = sorce;
    }
}

テストクラスDemo
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/*
* Comparable Comparator       
*    Comparable Comparator                   
*       ,                    
*
*   (String)      (int)   (float)
* liusan             20         90.0F
* lisi               22         90.0F
* wangwu             20         99.0F
* sunliu             22         100.0F
* */
public class Demo {
    public static void main(String[] args) {
        //             Student      ,             
        ArrayList stu=new ArrayList<>();
        Student stu1=new Student("liusan",22,90.0f);
        Student stu2=new Student("lisi",20,90.0f);
        Student stu3=new Student("wangwu",20,99.0f);
        Student stu4=new Student("sunliu",22,100.0f);
        stu.add(stu1);stu.add(stu2);stu.add(stu3);stu.add(stu4);
        //  Comparable
     /*   Collections.sort(stu);
        System.out.println("name\t\tage\t\tscore");
        for (Student student : stu) {
            System.out.println(student.getName()+"\t\t"+student.getAge()+"\t\t"+student.getSorce());
        }*/
        //  Comparator
       Collections.sort(stu, new Comparator() {
           @Override
           public int compare(Student o1, Student o2) {
               int m=(int) ((o2.getSorce()-o1.getSorce())*100);//       
               m=m==0?(o1.getAge()-o2.getAge()):m;//    ,                    
               return m;
           }
       });
       //    
        System.out.println("name\t\tage\t\tscore");
        for (Student student : stu) {
            System.out.println(student.getName()+"\t\t"+student.getAge()+"\t\t"+student.getSorce());
        }
    }
}
         ,                  。