小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

比較器comparable 和 comparator

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

一,比較器

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

  1. comparable

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

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

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

  用法場景:
  如果這個類是我們自己寫的,需要比較排序, 實現(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
     * 將此對象與指定對象進行大小比較。當此對象小于、等于或大于指定對象時,返回負整數(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);
//這個地方要求傳入一個Compartor, 如果傳入是個null, arraylist將會自動調(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);
    }
}

 

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

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多