zoukankan      html  css  js  c++  java
  • 比较器Comparator

    public static class IdAscendingComparator implements Comparator<Student> {
    
            //返回负数的时候,第一个参数排在前面
            //返回正数的时候,第二个参数排在前面
            //返回0的时候,谁在前面无所谓
            @Override
            public int compare(Student o1, Student o2) {
    //            if (o1.id < o2.id) {
    //                return -1;
    //            } else if (o2.id < o1.id) {
    //                return 1;
    //            }
    //            return 0;
                return o1.id - o2.id;  //升序
                //return o2.id - o1.id; //降序
            }
        }

    例子

    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.PriorityQueue;
    import java.util.TreeSet;
    
    public class Code_Comparator {
    
        public static class Student {
            public String name;
            public int id;
            public int age;
    
            public Student(String name, int id, int age) {
                this.name = name;
                this.id = id;
                this.age = age;
            }
        }
    
        public static class IdAscendingComparator implements Comparator<Student> {
    
            //返回负数的时候,第一个参数排在前面
            //返回正数的时候,第二个参数排在前面
            //返回0的时候,谁在前面无所谓
            @Override
            public int compare(Student o1, Student o2) {
    //            if (o1.id < o2.id) {
    //                return -1;
    //            } else if (o2.id < o1.id) {
    //                return 1;
    //            }
    //            return 0;
                return o1.id - o2.id;  //升序
                //return o2.id - o1.id; //降序
            }
        }
    
        public static class IdDescendingComparator implements Comparator<Student> {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.id - o1.id;
            }
    
        }
    
        public static class AgeAscendingComparator implements Comparator<Student> {
    
            @Override
            public int compare(Student o1, Student o2) {
                return o1.age - o2.age;
            }
    
        }
    
        public static class AgeDescendingComparator implements Comparator<Student> {
    
            @Override
            public int compare(Student o1, Student o2) {
                return o2.age - o1.age;
            }
    
        }
    
        public static void printStudents(Student[] students) {
            for (Student student : students) {
                System.out.println("Name : " + student.name + ", Id : " + student.id + ", Age : " + student.age);
            }
        }
    
        public static void printArray(Integer[] arr) {
            if (arr == null) {
                return;
            }
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
        }
    
        public static class MyComp implements Comparator<Integer> {
    
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
    
        }
    
        public static class Acomp implements Comparator<Integer> {
    
            //返回负数的时候,认为第一个参数排在前面
            //返回正数的时候,认为第二个参数排在前面
            //返回0的时候,谁在前面无所谓
            @Override
            public int compare(Integer arg0, Integer arg1) {
    
                return arg1 - arg0;
    //            return 0;
            }
        }
    
        public static void main(String[] args) {
            Student student1 = new Student("A", 2, 23);
            Student student2 = new Student("B", 3, 21);
            Student student3 = new Student("C", 1, 22);
    
            Student[] students = new Student[]{student1, student2, student3};
    
            Arrays.sort(students, new IdAscendingComparator());
            printStudents(students);
            /**
             * Name : C, Id : 1, Age : 22
             * Name : A, Id : 2, Age : 23
             * Name : B, Id : 3, Age : 21
             */
            System.out.println("===========================");
    
            Arrays.sort(students, new IdDescendingComparator());
            printStudents(students);
            /**
             * Name : B, Id : 3, Age : 21
             * Name : A, Id : 2, Age : 23
             * Name : C, Id : 1, Age : 22
             */
            System.out.println("===========================");
    
            Arrays.sort(students, new AgeAscendingComparator());
            printStudents(students);
            /**
             * Name : B, Id : 3, Age : 21
             * Name : C, Id : 1, Age : 22
             * Name : A, Id : 2, Age : 23
             */
            System.out.println("===========================");
    
            Arrays.sort(students, new AgeDescendingComparator());
            printStudents(students);
            /**
             * Name : A, Id : 2, Age : 23
             * Name : C, Id : 1, Age : 22
             * Name : B, Id : 3, Age : 21
             */
            System.out.println("===========================");
            System.out.println("===========================");
            System.out.println("===========================");
            System.out.println("===========================");
    
            PriorityQueue<Student> maxHeapBasedAge = new PriorityQueue<>(new AgeDescendingComparator());
            maxHeapBasedAge.add(student1);
            maxHeapBasedAge.add(student2);
            maxHeapBasedAge.add(student3);
            while (!maxHeapBasedAge.isEmpty()) {
                Student student = maxHeapBasedAge.poll();
                System.out.println("Name : " + student.name + ", Id : " + student.id + ", Age : " + student.age);
            }
            /**
             * Name : A, Id : 2, Age : 23
             * Name : C, Id : 1, Age : 22
             * Name : B, Id : 3, Age : 21
             */
            System.out.println("===========================");
    
            PriorityQueue<Student> minHeapBasedId = new PriorityQueue<>(new IdAscendingComparator());
            minHeapBasedId.add(student1);
            minHeapBasedId.add(student2);
            minHeapBasedId.add(student3);
            while (!minHeapBasedId.isEmpty()) {
                Student student = minHeapBasedId.poll();
                System.out.println("Name : " + student.name + ", Id : " + student.id + ", Age : " + student.age);
            }
            /**
             * Name : C, Id : 1, Age : 22
             * Name : A, Id : 2, Age : 23
             * Name : B, Id : 3, Age : 21
             */
            System.out.println("===========================");
            System.out.println("===========================");
            System.out.println("===========================");
    
            TreeSet<Student> treeAgeDescending = new TreeSet<>(new AgeDescendingComparator());
            treeAgeDescending.add(student1);
            treeAgeDescending.add(student2);
            treeAgeDescending.add(student3);
    
            Student studentFirst = treeAgeDescending.first();
            System.out.println("Name : " + studentFirst.name + ", Id : " + studentFirst.id + ", Age : " + studentFirst.age);
            // Name : A, Id : 2, Age : 23
    
            Student studentLast = treeAgeDescending.last();
            System.out.println("Name : " + studentLast.name + ", Id : " + studentLast.id + ", Age : " + studentLast.age);
            //Name : B, Id : 3, Age : 21
            System.out.println("===========================");
    
        }
    
    }
  • 相关阅读:
    Codesys——限定符的使用方法[来自Codesys的Help]
    分页后台
    多条件查询判断
    添加跟反射
    试图页面分页首选
    动态游标存储过程 表名为参数
    索引器
    泛型 Generics
    Win10 锁屏图片 路径
    SQL2014 error 40 ( Microsoft SQL Server, 错误2)
  • 原文地址:https://www.cnblogs.com/zh-xiaoyuan/p/15144760.html
Copyright © 2011-2022 走看看