1 相同点
无论是Comparable接口还是Comparator接口都是为了实现对象的可比较性,之后通过对象间的比较进而通过Collection.sort()或者Arrays.sort()方法对集合或对象数组进行排序。
2 区别
1.实现方法
Comparable接口需要实现int compareTo(T t)方法。
Comparator接口实现的是int compare(Object o1,Object o2)方法。
2.存放位置
Comparable接口存放于java.lang中。
Comparator接口存放于java.util中。
3.使用方式(见代码实例)
Comparable接口是实现于实体类中,如果需要修改比较方式则需要修改实体类的源代码。
Comparator接口独立实现于外部,通过传入比较器实例从而实现对象的排序。
3 Comparable代码实例
场景:学生实体有年龄、成绩等属性,现在需要提供一个排序的方法。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 使用Comparable接口实现对象的排序功能
* @author hong
*
*/
public class ComparableTest {
public static void main(String[] args) {
ArrayList<Student> stuList = new ArrayList<>();
Student stu1 = new Student("Tom", 20 , 72);
Student stu2 = new Student("Jerry", 21, 92);
Student stu3 = new Student("Hery", 21, 80);
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
System.out.println("未排序的情况:");
showStudentsList(stuList);
Collections.sort(stuList); // 排序
System.out.println("排序后的情况:");
showStudentsList(stuList);
}
/**
* 展示学生列表信息
* @param list
*/
public static void showStudentsList(List<Student> list) {
for(Student stu: list) {
System.out.println(stu.toString());
}
System.out.println();
}
/**
* Student类实现对象的可比较性
* @author hong
*
*/
public static class Student implements Comparable {
// 此处偷懒使用public修饰,推荐使用private修饰再通过访问器访问
public String name;
public int age;
public int score;
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public int compareTo(Object o) { // 实现对象间通过score属性的比较
if(o.getClass() == Student.class) {
Student stu = (Student)o;
return stu.score - this.score;
}
return 0;
}
@Override
public String toString() {
String mes = "【"+this.name+","+this.age+","+this.score+"】";
return mes;
}
}
}
运行结果
未排序的情况:
【Tom,20,72】
【Jerry,21,92】
【Hery,21,80】
根据分数排序后的情况:
【Jerry,21,92】
【Hery,21,80】
【Tom,20,72】
4 Comparator代码实例
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparatorTest {
public static void main(String[] args) {
ArrayList<Student> stuList = new ArrayList<>();
Student stu1 = new Student("Tom", 19 , 72);
Student stu2 = new Student("Jerry", 21, 92);
Student stu3 = new Student("Hery", 23, 80);
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
System.out.println("未排序的情况:");
showStudentsList(stuList);
Collections.sort(stuList, new ScoreComparator()); // 根据分数排序
System.out.println("根据分数排序后的情况:");
showStudentsList(stuList);
Collections.sort(stuList, new AgeComparator()); // 根据年龄排序
System.out.println("根据年龄排序后的情况:");
showStudentsList(stuList);
}
/**
* 展示学生列表信息
* @param list
*/
public static void showStudentsList(List<Student> list) {
for(Student stu: list) {
System.out.println(stu.toString());
}
System.out.println();
}
/**
* Student类
* @author hong
*
*/
public static class Student {
// 此处偷懒使用public修饰,推荐使用private修饰再通过访问器访问
public String name;
public int age;
public int score;
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
String mes = "【"+this.name+","+this.age+","+this.score+"】";
return mes;
}
}
/**
* 学生分数比较器
* @author hong
*
*/
public static class ScoreComparator implements Comparator<Student> {
@Override
public int compare(Student stu1, Student stu2) {
return stu1.score - stu2.score;
}
}
/**
* 学生年龄比较器
* @author hong
*
*/
public static class AgeComparator implements Comparator<Student> {
@Override
public int compare(Student stu1, Student stu2) {
return stu1.age - stu2.age;
}
}
}
运行结果
未排序的情况:
【Tom,19,72】
【Jerry,21,92】
【Hery,23,80】
根据分数排序后的情况:
【Tom,19,72】
【Hery,23,80】
【Jerry,21,92】
根据年龄排序后的情况:
【Tom,19,72】
【Jerry,21,92】
【Hery,23,80】