比较器

  1. // 任何比较器:
  2. // compare方法里,遵循一个统一的规范:
  3. // 返回负数的时候,认为第一个参数应该排在前面
  4. // 返回正数的时候,认为第二个参数应该排在前面
  5. // 返回0的时候,认为无所谓谁放前面
  6. public static class IdShengAgeJiangOrder implements Comparator<Student> {
  7. // 根据id从小到大,但是如果id一样,按照年龄从大到小
  8. @Override
  9. public int compare(Student o1, Student o2) {
  10. return o1.id != o2.id ? (o1.id - o2.id) : (o2.age - o1.age);
  11. }
  12. }
  13. public static class IdAscendingComparator implements Comparator<Student> {
  14. // 返回负数的时候,第一个参数排在前面
  15. // 返回正数的时候,第二个参数排在前面
  16. // 返回0的时候,谁在前面无所谓
  17. @Override
  18. public int compare(Student o1, Student o2) {
  19. return o1.id - o2.id;
  20. }
  21. }
  22. public static class IdDescendingComparator implements Comparator<Student> {
  23. @Override
  24. public int compare(Student o1, Student o2) {
  25. return o2.id - o1.id;
  26. }
  27. }
  28. // 先按照id排序,id小的,放前面;
  29. // id一样,age大的,前面;
  30. public static class IdInAgeDe implements Comparator<Student> {
  31. @Override
  32. public int compare(Student o1, Student o2) {
  33. return o1.id != o2.id ? o1.id - o2.id : (o2.age - o1.age);
  34. }
  35. }
  36. public static void printStudents(Student[] students) {
  37. for (Student student : students) {
  38. System.out.println("Name : " + student.name + ", Id : " + student.id + ", Age : " + student.age);
  39. }
  40. }
  41. public static void printArray(Integer[] arr) {
  42. if (arr == null) {
  43. return;
  44. }
  45. for (int i = 0; i < arr.length; i++) {
  46. System.out.print(arr[i] + " ");
  47. }
  48. System.out.println();
  49. }
  50. public static class MyComp implements Comparator<Integer> {
  51. @Override
  52. public int compare(Integer o1, Integer o2) {
  53. return o2 - o1;
  54. }
  55. }
  56. public static class AComp implements Comparator<Integer> {
  57. // 如果返回负数,认为第一个参数应该拍在前面
  58. // 如果返回正数,认为第二个参数应该拍在前面
  59. // 如果返回0,认为谁放前面都行
  60. @Override
  61. public int compare(Integer arg0, Integer arg1) {
  62. return arg1 - arg0;
  63. // return 0;
  64. }
  65. }
  66. public static void main(String[] args) {
  67. Integer[] arr = { 5, 4, 3, 2, 7, 9, 1, 0 };
  68. Arrays.sort(arr, new AComp());
  69. for (int i = 0; i < arr.length; i++) {
  70. System.out.println(arr[i]);
  71. }
  72. System.out.println("===========================");
  73. Student student1 = new Student("A", 4, 40);
  74. Student student2 = new Student("B", 4, 21);
  75. Student student3 = new Student("C", 3, 12);
  76. Student student4 = new Student("D", 3, 62);
  77. Student student5 = new Student("E", 3, 42);
  78. // D E C A B
  79. Student[] students = new Student[] { student1, student2, student3, student4, student5 };
  80. System.out.println("第一条打印");
  81. Arrays.sort(students, new IdShengAgeJiangOrder());
  82. for (int i = 0; i < students.length; i++) {
  83. Student s = students[i];
  84. System.out.println(s.name + "," + s.id + "," + s.age);
  85. }
  86. System.out.println("第二条打印");
  87. ArrayList<Student> studentList = new ArrayList<>();
  88. studentList.add(student1);
  89. studentList.add(student2);
  90. studentList.add(student3);
  91. studentList.add(student4);
  92. studentList.add(student5);
  93. studentList.sort(new IdShengAgeJiangOrder());
  94. for (int i = 0; i < studentList.size(); i++) {
  95. Student s = studentList.get(i);
  96. System.out.println(s.name + "," + s.id + "," + s.age);
  97. }
  98. // N * logN
  99. System.out.println("第三条打印");
  100. student1 = new Student("A", 4, 40);
  101. student2 = new Student("B", 4, 21);
  102. student3 = new Student("C", 4, 12);
  103. student4 = new Student("D", 4, 62);
  104. student5 = new Student("E", 4, 42);
  105. TreeMap<Student, String> treeMap = new TreeMap<>((a, b) -> (a.id - b.id));
  106. treeMap.put(student1, "我是学生1,我的名字叫A");
  107. treeMap.put(student2, "我是学生2,我的名字叫B");
  108. treeMap.put(student3, "我是学生3,我的名字叫C");
  109. treeMap.put(student4, "我是学生4,我的名字叫D");
  110. treeMap.put(student5, "我是学生5,我的名字叫E");
  111. for (Student s : treeMap.keySet()) {
  112. System.out.println(s.name + "," + s.id + "," + s.age);
  113. }
  114. }