Comparable接口和Comparator接口
1、一个类在设计之初就要实现对该类对象的排序功能,那么这个类要实现Comparable接口,实现public int compareTo(T t)方法。如代码中的Student类。
对于实现Comparable接口的类,
调用java.util.Arrays.sort(Object[] a)对包含对象实例的数组进行排序,
调用java.util.Collections.sort(List<T> list)对包含对象实例的list进行排序。
2、若一个类在设计之初并没有对排序功能的需求,而是在后续的使用中想要对这个类添加排序的功能,如Teacher类。这时的办法是实现一个比较器类,
如TeacherComparator类,实现public int compare(T t1, T t2)让该类实现Comparator接口。
调用java.util.Arrays.sort(T[] a, Comparator<? super T> c) 对包含对象实例的数组进行排序,
调用java.util.Collections.sort(List<T> list, Comparator<? super T> c) 对包含对象实例的list进行排序。
3、a.compareTo(b)方法和compare(a,b)方法,在方法中判断两个对象的某个或某些属性的大小关系,若a的某个属性比b的属性小,返回负整数,若相等,返回0,否则返回正整数。
if a.attr < b.attr
return -1
else
return 1
上面即为按照某属性从小到大排序。
if a.attr < b.attr
return 1
else
return -1
上面为按照某属性从大到小排序。
以下代码按照Score从高到底排序,若Score相同,则按照age从小到大排序。
public class Student implements Comparable<Student>{ private String name; private int id; private int score; private int age; public Student(String name, int id, int score, int age) { super(); this.name = name; this.id = id; this.score = score; this.age = age; } @Override public int compareTo(Student s) { if (this.score < s.getScore()) { return 1; } else if (this.score > s.getScore()) { return -1; } else { if (this.age < s.getAge()) { return -1; } else { return 1; } } } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String[] args) { System.out.println("use Interface Comparable"); System.out.println(); System.out.println("Array "); System.out.println(); Student[] stu = {new Student("a", 1, 88, 18), new Student("b", 2, 78, 16), new Student("c", 3, 88, 17), new Student("d", 4, 78, 16), new Student("e", 5, 100, 18)}; Arrays.sort(stu); for (Student s : stu) { System.out.println(s.getScore() + " " + s.getAge()); } System.out.println("***"); System.out.println("List"); System.out.println(); List<Student> list = new ArrayList<Student>(); list.add(new Student("a", 1, 88, 18)); list.add(new Student("b", 2, 78, 16)); list.add(new Student("c", 3, 88, 17)); list.add(new Student("d", 4, 78, 16)); list.add(new Student("e", 5, 100, 18)); Collections.sort(list); for (Student s : list) { System.out.println(s.getScore() + " " + s.getAge()); } System.out.println(); System.out.println("use Interface Comparator"); System.out.println(); System.out.println("Array"); System.out.println(); Teacher[] teach = { new Teacher("a", 1, 88, 18), new Teacher("b", 2, 78, 16), new Teacher("c", 3, 88, 17), new Teacher("d", 4, 78, 16), new Teacher("e", 5, 100, 18)}; Arrays.sort(teach, new TeacherComparator()); for (Teacher t : teach) { System.out.println(t.getScore() + " " + t.getAge()); } System.out.println(); System.out.println("List"); System.out.println(); List<Teacher> tList = new ArrayList<Teacher>(); tList.add(new Teacher("a", 1, 88, 18)); tList.add(new Teacher("b", 2, 78, 16)); tList.add(new Teacher("c", 3, 88, 17)); tList.add(new Teacher("d", 4, 78, 16)); tList.add(new Teacher("e", 5, 100, 18)); Collections.sort(tList, new TeacherComparator()); for (Teacher t : tList) { System.out.println(t.getScore() + " " + t.getAge()); } } } class Teacher { private String name; private int id; private int score; private int age; public Teacher(String name, int id, int score, int age) { super(); this.name = name; this.id = id; this.score = score; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } class TeacherComparator implements Comparator<Teacher> { @Override public int compare(Teacher t1, Teacher t2) { if (t1.getScore() < t2.getScore()) { return 1; } else if (t1.getScore() > t2.getScore()) { return -1; } else { if (t1.getAge() < t2.getAge()) { return -1; } else { return 1; } } } }