ComprableとCompratorの2つのインターフェースを使ってカスタムソート操作を実現します.

4006 ワード

一、Comprableを使う
   1、学生類をカプセル化し、Comprableインターフェースを実現する.compreToを書き換える方法です.並べ替え規則をカスタマイズします.
import java.util.Objects;

public class Student implements Comparable {
    private String name;
    private int age;
     private float grades;

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

    public Student() {
    }

    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 getGrades() {
        return grades;
    }

    public void setGrades(float grades) {
        this.grades = grades;
    }

    @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.grades, grades) == 0 &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, grades);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", grades=" + grades +
                '}';
    }
    //       
    @Override
    public int compareTo(Student o) {
        int n1=(int)(this.grades-o.grades);
        int n2=this.age-o.age;
        return n1==0?n2:n1;
    }
}
2、試験類の作成
        4人の学生の成績は昇順に並べられています.成績が同じなら、年齢によって順位をつけます.
public class Demo3 {
    public static void main(String[] args) {
        TreeSet treeSet=new TreeSet<>();
            Student s1=new Student("  ", 20, 90.0f);
            Student s2=new Student("  ", 22, 90.0f);
            Student s3=new Student("  ", 20, 99.0f);
            Student s4=new Student("  ", 22, 100.0f);
               treeSet.add(s1);
               treeSet.add(s2);
               treeSet.add(s3);
               treeSet.add(s4);
             System.out.println(treeSet);
        System.out.println("-----------------------------");
        for (Student student : treeSet) {
            System.out.println(student);
        }


    }
}
二、Compratorを使って並べ替えを実現します.
  テストクラス:
public class Demo3 {
    public static void main(String[] args) {
        TreeSet treeSet=new TreeSet<>(new Comparator() {
            @Override
            public int compare(Student o1, Student o2) {
                int n1=(int) (o1.getGrades()-o2.getGrades());
                int n2=o1.getAge()-o2.getAge();
                return n1==0?n2:n1;
            }
        });
            Student s1=new Student("  ", 20, 90.0f);
            Student s2=new Student("  ", 22, 90.0f);
            Student s3=new Student("  ", 20, 99.0f);
            Student s4=new Student("  ", 22, 100.0f);
               treeSet.add(s1);
               treeSet.add(s2);
               treeSet.add(s3);
               treeSet.add(s4);
             System.out.println(treeSet);
        System.out.println("-----------------------------");
        for (Student student : treeSet) {
            System.out.println(student);
        }



    }
}
 コアコード:
 TreeSet treeSet=new TreeSet<>(new Comparator() {
            @Override
            public int compare(Student o1, Student o2) {
                int n1=(int) (o1.getGrades()-o2.getGrades());
                int n2=o1.getAge()-o2.getAge();
                return n1==0?n2:n1;
            }
        });