比较器:
Comparable: 内部比较器,实现相对简单 缺点是要修改源代码
Comparator: 外部比较器,优点是不需要修改源代码
Comparable: 内部比较器
package Content; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collections; import java.util.TreeSet; //Comparable: 内部比较器 public class CollectionSortDemo_Comparable { static class Student implements Comparable<Student>{ String name; int mark; public Student (String name,int mark) { this.name = name; this.mark = mark; } public int compareTo(Student oStudent) { //this>o 从小到大 if(this.mark>oStudent.mark) { return 1; }else if(this.mark<oStudent.mark) { return -1; }else { return this.name.compareTo(oStudent.name); } } } public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jhon"); arrayList.add("Tom"); arrayList.add("Jerry"); arrayList.add("Anna"); System.out.println(arrayList); //排序 Collections.sort(arrayList); System.out.println(arrayList); System.out.println(); ArrayList<Integer> arrayList2 = new ArrayList<Integer>(); arrayList2.add(6); arrayList2.add(5); arrayList2.add(1); arrayList2.add(3); arrayList2.add(2); System.out.println(arrayList2); Collections.sort(arrayList2); System.out.println(arrayList2); System.out.println(); ArrayList<Student> studentlist = new ArrayList<>(); studentlist.add(new Student("Jhon", 80)); studentlist.add(new Student("Tom", 60)); studentlist.add(new Student("Jerry", 90)); studentlist.add(new Student("Amy", 90)); studentlist.add(new Student("Anna", 100)); for(Student e:studentlist) { System.out.println(e.name+" "+e.mark); } System.out.println(); System.out.println("Sort:"); Collections.sort(studentlist); for(Student e:studentlist) { System.out.println(e.name+" "+e.mark); } System.out.println(); System.out.println("TreeSet:"); TreeSet<Student> studentSet = new TreeSet<>(); studentSet.add(new Student("Jhon", 80)); studentSet.add(new Student("Tom", 60)); studentSet.add(new Student("Jerry", 90)); studentSet.add(new Student("Amy", 90)); studentSet.add(new Student("Anna", 100)); for(Student e:studentSet) { System.out.println(e.name+" "+e.mark); } } }
Comparator: 外部比较器
package Content; import java.util.ArrayList; import java.util.Comparator; import java.util.TreeSet; //Comparator: 外部比较器 public class CollectionSortDemo_Comparator { static class Employee { String name; double salary; public Employee(String name,double salary) { this.name = name; this.salary = salary; } } static class EmployeeComparator implements Comparator<Employee>{ public int compare(Employee oEmployee, Employee oEmployee2) { if (oEmployee.salary> oEmployee2.salary) { return 1; } else if (oEmployee.salary < oEmployee2.salary) { return -1; } else { return oEmployee.name.compareTo(oEmployee2.name); } } } public static void main(String[] args) { TreeSet<Employee> employeesSet = new TreeSet<>(new EmployeeComparator()); employeesSet.add(new Employee("Jhon", 8000)); employeesSet.add(new Employee("Tom", 6000)); employeesSet.add(new Employee("Jerry", 9000)); employeesSet.add(new Employee("Anna", 10000)); for(Employee e:employeesSet) { System.out.println(e.name+" "+e.salary); } // Collections.sort(employeeslist,new EmployeeComparator()); } }