其他分享
首页 > 其他分享> > 12:比较器

12:比较器

作者:互联网

12:比较器

 

1、比较器的实质是重载比较运算符;

2、比较器可以很好的应用在特殊标准的排序上;

3、比较器可以很好的应用在根据特殊标准的结构上;

4、写代码异常容易,还用于泛型编程;

 

 

 1 public static class Student {
 2         public String name;
 3         public int id;
 4         public int age;
 5 
 6         public Student(String name, int id, int age) {
 7             this.name = name;
 8             this.id = id;
 9             this.age = age;
10         }
11     }
12 
13     // 任何比较器:
14     // compare方法里,遵循一个统一的规范:
15     // 返回负数的时候,认为第一个参数应该排在前面
16     // 返回正数的时候,认为第二个参数应该排在前面
17     // 返回0的时候,认为无所谓谁放前面
18     public static class IdShengAgeJiangOrder implements Comparator<Student> {
19 
20         // 根据id从小到大,但是如果id一样,按照年龄从大到小
21         @Override
22         public int compare(Student o1, Student o2) {
23             return o1.id != o2.id ? (o1.id - o2.id) : (o2.age - o1.age);
24         }
25 
26     }

 

 1 public static void main(String[] args) {
 2 
 3         Integer[] arr = { 5, 4, 3, 2, 7, 9, 1, 0 };
 4 
 5         Arrays.sort(arr, new AComp());
 6 
 7         for (int i = 0; i < arr.length; i++) {
 8             System.out.println(arr[i]);
 9         }
10 
11         System.out.println("===========================");
12 
13         Student student1 = new Student("A", 4, 40);
14         Student student2 = new Student("B", 4, 21);
15         Student student3 = new Student("C", 3, 12);
16         Student student4 = new Student("D", 3, 62);
17         Student student5 = new Student("E", 3, 42);
18         // D E C A B
19 
20         Student[] students = new Student[] { student1, student2, student3, student4, student5 };
21         System.out.println("第一条打印");
22 
23         Arrays.sort(students, new IdShengAgeJiangOrder());
24         for (int i = 0; i < students.length; i++) {
25             Student s = students[i];
26             System.out.println(s.name + "," + s.id + "," + s.age);
27         }
28 
29         System.out.println("第二条打印");
30         ArrayList<Student> studentList = new ArrayList<>();
31         studentList.add(student1);
32         studentList.add(student2);
33         studentList.add(student3);
34         studentList.add(student4);
35         studentList.add(student5);
36         studentList.sort(new IdShengAgeJiangOrder());
37         for (int i = 0; i < studentList.size(); i++) {
38             Student s = studentList.get(i);
39             System.out.println(s.name + "," + s.id + "," + s.age);
40         }
41         // N * logN
42         System.out.println("第三条打印");
43         student1 = new Student("A", 4, 40);
44         student2 = new Student("B", 4, 21);
45         student3 = new Student("C", 4, 12);
46         student4 = new Student("D", 4, 62);
47         student5 = new Student("E", 4, 42);
48         TreeMap<Student, String> treeMap = new TreeMap<>((a, b) -> (a.id - b.id));
49         treeMap.put(student1, "我是学生1,我的名字叫A");
50         treeMap.put(student2, "我是学生2,我的名字叫B");
51         treeMap.put(student3, "我是学生3,我的名字叫C");
52         treeMap.put(student4, "我是学生4,我的名字叫D");
53         treeMap.put(student5, "我是学生5,我的名字叫E");
54         for (Student s : treeMap.keySet()) {
55             System.out.println(s.name + "," + s.id + "," + s.age);
56         }
57 
58     }
59 
60 }

 

标签:12,age,id,public,studentList,Student,new,比较
来源: https://www.cnblogs.com/yzmarcus/p/16275728.html