电竞比分网-中国电竞赛事及体育赛事平台

分享

比較器comparable 和 comparator

 印度阿三17 2022-12-26 發(fā)布于重慶
標(biāo)簽:sort comparable comparator Point students public Student new 比較

一,比較器

  比較變量之間的大小  ->  排序

  1. comparable

    此接口對實(shí)現(xiàn)它的每個(gè)類的對象強(qiáng)制執(zhí)行排序。
    實(shí)現(xiàn)此接口的對象的數(shù)組, 可以通過Arrays.sort自動(dòng)排序

    comparable中有個(gè)compareTo方法
    將此對象與指定對象進(jìn)行順序比較。當(dāng)此對象小于、等于或大于指定對象時(shí),返回負(fù)整數(shù)、零或正整數(shù)

  2. comparator
    一個(gè)比較接口,它對某些對象集合進(jìn)行排序。
    可以將比較器(new了一個(gè)實(shí)現(xiàn)了comparator接口的對象)傳遞給排序方法(例如Collections.sort或Arrays.sort)

  用法場景:
  如果這個(gè)類是我們自己寫的,需要比較排序, 實(shí)現(xiàn)Comprable, 如果不是我們自己寫的代碼, 我們無法修改的代碼, 通過comparator

二,Comparable

  

public class Student implements Comparable<Student>{

    public String name;
    public Integer score;
    public Integer age;

    public Student(String name, Integer score, Integer age) {
        this.name = name;
        this.score = score;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", score=" + score +
                ", age=" + age +
                '}';
    }

    /**
     * int aInt = gaoPeng.compareTo(tuEnXia);
     * 此對象指的是gaoPeng, 指定對象指的是tuEnXia
     * 將此對象與指定對象進(jìn)行大小比較。當(dāng)此對象小于、等于或大于指定對象時(shí),返回負(fù)整數(shù)、零或正整數(shù)
     * @return
     */
    @Override
    public int compareTo(Student other) {
        int diff = this.score - other.score;
        return diff;
    }
}

 

public class ComparableDemo01 {
    public static void main(String[] args) {
        Student student1 = new Student("張三", 700, 19);
        Student student2 = new Student("李四", 666, 18);
        Student student3 = new Student("王麻子", 750, 19);
//        //1. 數(shù)組排序
//        Student[] students = {student1,student2,student3};
//        Arrays.sort(students);
//        System.out.println(Arrays.toString(students));

        //2, 集合的comparable排序
        ArrayList<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        students.add(student3);
//        Collections.sort(students);
//        System.out.println(students);
//這個(gè)地方要求傳入一個(gè)Compartor, 如果傳入是個(gè)null, arraylist將會(huì)自動(dòng)調(diào)用本集合中對象的comparable方法
        students.sort(null);
        System.out.println(students);
    }
}

 

三,Comparator

  

public class PointComparator implements Comparator<Point> {

    @Override
    public int compare(Point point1, Point point2) {
        int diff = point1.x + point1.y - point2.x - point2.y;
        return diff;
    }
}

 

public class PointComparator {
    public static  void main(String[] args) {
        Point point1 = new Point(44, 55);
        Point point2 = new Point(58, 56);
        Point point3 = new Point(58, 96);
        //1,數(shù)組排序
//        Point[] points = {point1,point2,point3};
//        Arrays.sort(points,new PointComparator());
//        System.out.println(Arrays.toString(points));
        //2 集合排序
        LinkedList<Point> linkedList = new LinkedList<>();
        linkedList.add(point1);
        linkedList.add(point2);
        linkedList.add(point3);
//        Collections.sort(linkedList,new PointComparator());
//       linkedList.sort(new PointComparator());
//        System.out.println(linkedList);
        //3 通過匿名內(nèi)部類排序
        Comparator<Point> comparator = new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                int diff = o1.x + o1.y - o2.x - o2.y;
                return diff;
            }
        };
//        linkedList.sort(comparator);
        Collections.sort(linkedList,comparator);
        System.out.println(linkedList);
    }
}

 

標(biāo)簽:sort,comparable,comparator,Point,students,public,Student,new,比較
來源: https://www./show/4/169714.html  

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多