首页 > 其他分享 >比较器comparable 和 comparator

比较器comparable 和 comparator

时间:2022-12-26 15:37:00浏览次数:43  
标签:sort comparable comparator Point students public Student new 比较

一,比较器

  比较变量之间的大小  ->  排序

  1. comparable

    此接口对实现它的每个类的对象强制执行排序。
    实现此接口的对象的数组, 可以通过Arrays.sort自动排序

    comparable中有个compareTo方法
    将此对象与指定对象进行顺序比较。当此对象小于、等于或大于指定对象时,返回负整数、零或正整数

  2. comparator
    一个比较接口,它对某些对象集合进行排序。
    可以将比较器(new了一个实现了comparator接口的对象)传递给排序方法(例如Collections.sort或Arrays.sort)

  用法场景:
  如果这个类是我们自己写的,需要比较排序, 实现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
     * 将此对象与指定对象进行大小比较。当此对象小于、等于或大于指定对象时,返回负整数、零或正整数
     * @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. 数组排序
//        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将会自动调用本集合中对象的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,数组排序
//        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 通过匿名内部类排序
        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,比较
From: https://www.cnblogs.com/p1121/p/17005893.html

相关文章