首先,不论是compare(T t1, T t2),还是compareTo(T t2)方法,方法返回如果是正值,则代表前者是数值比较大的一方,比如上面的t1和compareTo方法的调用者,如果返回是0则代表两者相同,如果返回负数,则表示t2数值较大,如果想要降序排列,只要让结果相反即可。
Comparable接口:
说明:
Comparable接口的作用是使某种类具有比较的功能,例如java的主数据类型的封装类和String都实现了Comparable类。
Comparable接口只有一个compareTo(T t)方法,实现类需要在该方法中定义实现类的比较逻辑。
demo:
Person类,实现Comparable接口,以年龄大小作为比较的属性
public class Person implements Comparable{
public Person(String name, Integer age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
private String name;
private Integer age;
private String sex;
@Override
public int compareTo(Person o) {
return this.age.compareTo(o.getAge());
}
// getter 和 setter
}
测试:
public class Test {
public static void main(String[] args) {
Person person = new Person("a", 199, "male");
Person person1 = new Person("b", 15, "female");
System.out.println(person.compareTo(person1));
}
}
// 打印 1
ps:在字符和字符串比较时,其实是比较字符的ASCLL码值,返回的是两个值相减后的结果(前者减去后者)。
Comparator接口
Comparator接口是在集合类排序时使用的一个比较器,一般在集合排序时作为参数传入。
demo
Student类:
public class Student {
public Student(String name, String school, String major) {
this.name = name;
this.school = school;
this.major = major;
}
private String name;
private String school;
private String major;
// getter和setter省略
}
测试
public class Test {
public static void main(String[] args) {
Student[] student = new Student[7];
student[0] = new Student("le", "jian xue Street", "net security");
student[1] = new Student("tu", "jian xue Street", "net security");
student[2] = new Student("hou", "jian xue Street", "net security");
student[3] = new Student("jin", "jian xue Street", "net security");
student[4]= new Student("wu", "jian xue Street", "net security");
student[5] = new Student("wang", "jian xue Street", "net security");
student[6] = new Student("ang", "jian xue Street", "net security");
ArrayList students = new ArrayList<>(10);
students.addAll(Arrays.asList(student));
students.sort(Comparator.comparing(Student::getName));
students.forEach(System.out::println);
}
// 结果list中得到对象按name正序排列
Comparator接口中定义了几个静态方法生成可用的Comparator
// 返回一个与自然排序相反的比较器,一般用在实现了Comparable接口的对象集合中,比较元素中不能有null
public static > Comparator reverseOrder() {
return Collections.reverseOrder();
}
// 返回根据自然排序的比较器,一般用在实现了Comparable接口的对象集合中,比较元素中不能有null
public static > Comparator naturalOrder() {
return (Comparator) Comparators.NaturalOrderComparator.INSTANCE;
}
// 根据传入的比较器生成一个将null排在前面的比较器并返回
public static Comparator nullsFirst(Comparator super T> comparator) {
return new Comparators.NullComparator<>(true, comparator);
}
// 根据传入的比较器生成一个将null排在后面的比较器并返回
public static Comparator nullsLast(Comparator super T> comparator) {
return new Comparators.NullComparator<>(false, comparator);
}
// 根据指定的key和比较器生成应的比较器,第一个参数是获取比较的key的函数,第二个参数是指定的比较器
public static Comparator comparing(
Function super T, ? extends U> keyExtractor,
Comparator super U> keyComparator)
{
Objects.requireNonNull(keyExtractor);
Objects.requireNonNull(keyComparator);
return (Comparator & Serializable)
(c1, c2) -> keyComparator.compare(keyExtractor.apply(c1),
keyExtractor.apply(c2));
}
// 根据指定的key并使用该key对应的compareTo方法进行比较,参数为获取该key的函数
public static > Comparator comparing(
Function super T, ? extends U> keyExtractor)
{
Objects.requireNonNull(keyExtractor);
return (Comparator & Serializable)
(c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}
// 根据传入的获取比较key的函数返回一个使用Integer的conpare方法比较的比较器
public static Comparator comparingInt(ToIntFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator & Serializable)
(c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));
}
// 根据传入的获取比较key的函数返回一个使用Long的conpare方法比较的比较器
public static Comparator comparingLong(ToLongFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator & Serializable)
(c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2));
}
// 根据传入的获取比较key的函数返回一个使用Double的conpare方法比较的比较器
public static Comparator comparingDouble(ToDoubleFunction super T> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator & Serializable)
(c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
}
另外,Comparator还有一些默认的方法实现,提供了对基础比较的一些补充比较,即当本身的比较器比较完后,如果有两个对象相同的情况(返回值为0),则会调用传入的补充比较器。
例如
// 调用该方法,传入一个补充比较器,比较器本身调用compare返回0时,调用补充比较器比较
default Comparator thenComparing(Comparator super T> other) {
Objects.requireNonNull(other);
return (Comparator & Serializable) (c1, c2) -> {
int res = compare(c1, c2);
return (res != 0) ? res : other.compare(c1, c2);
};
}
还有一些默认方法也与上面类似,就不一一列举了。