java的比較器應用於很多場景,最常見的出泛型的排序或者是簡單的排序。有二種實現方式,使用Comparable、Comparator,哪一種符合具體的開發情形,還是要看二者的區別。從api上得知,一個類實現了Comparable接口則表明這個類的對象之間是可以相互比較的,這個類對象組成的集合就可以直接使用sort方法排序。Comparator可以看成一種算法的實現,將算法和數據分離,Comparator也可以在下面兩種環境下使用:
1、類的設計師沒有考慮到比較問題而沒有實現Comparable,可以通過Comparator來實現排序而不必改變對象本身
2、可以使用多種排序標準,比如升序、降序等。
總結下,個人偏愛第二種,因爲數據和功能的解耦體現了MVC的精神,會產生一定的效益。具體看下代碼體驗。
1.使用Comparator
public class MyRule implements Comparator<Student> {
private String order;
private String choice;
public MyRule(String order, String choice) {
this.order = order;
this.choice = choice;
}
@Override
public int compare(Student o1, Student o2) {
if (order.isEmpty()) {
System.out.println("order is empty");
return 0;
}
if (choice.isEmpty()) {
System.out.println("choice is empty");
return 0;
}
// 降序
if (order.equals("desc")) {
if (choice.equals("age")) {
return o2.getAge() - o1.getAge();
} else if (choice.equals("score")) {
return o2.getScore() - o1.getScore();
} else {
System.out.println("no such choice");
return 0;
}
}
// 升序
else if (order.equals("asc")) {
if (choice.equals("age")) {
return o1.getAge() - o2.getAge();
} else if (choice.equals("score")) {
return o1.getScore() - o2.getScore();
} else {
System.out.println("no such choice");
return 0;
}
} else {
System.out.println("no such order");
return 0;
}
}
}
public class Student {
private int userId;
private String username;
private int age;
private int score;
public Student(int userId, String username, int age, int score) {
this.userId = userId;
this.username = username;
this.age = age;
this.score = score;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student [userId=" + userId + ", username=" + username
+ ", age=" + age + ", score=" + score + "]";
}
}
public class SortDemo {
public static void main(String[] args) throws Exception {
List<Student> list = new ArrayList<Student>();
list.add(new Student(1, "a", 26, 97));
list.add(new Student(2, "b", 24, 80));
list.add(new Student(3, "c", 28, 91));
// 選擇降序排列成績
MyRule mr = new MyRule("desc", "score");
Collections.sort(list, mr);
for (Student userInfo : list) {
System.out.println(userInfo.toString());
}
}
}
2.使用Comparable
public class Person implements Comparable {
private int userId;
private String username;
private int age;
private int score;
public Person(int userId, String username, int age, int score) {
this.userId = userId;
this.username = username;
this.age = age;
this.score = score;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student [userId=" + userId + ", username=" + username
+ ", age=" + age + ", score=" + score + "]";
}
@Override
public int compareTo(Object o) {
return age - ((Person) o).getAge();
}
public static void main(String[] args) {
List<Person> data = new ArrayList<Person>();
data.add(new Person(1, "xigua", 22, 59));
data.add(new Person(2, "mugua", 32, 19));
data.add(new Person(3, "donggua", 27, 27));
Collections.sort(data);
for (Person person : data) {
System.out.println(person.toString());
}
}
}