比较器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("==========================="); } }