Java Comparator.thenComparing | 添加次级排序方式
- Comparator.thenComparing
- Comparator.thenComparingInt
- Comparator.thenComparingLong
- Comparator.thenComparingDouble
- 参考文献
thenComparing
是比较器功能接口的默认方法。
Comparator.thenComparing
方法是在Java 8
中引入的。
Comparator.thenComparing
返回一个词表顺序的比较器,该比较器被一个比较器实例调用,使用一组排序键对项目进行排序。
当这个比较器比较两个元素相等时,thenComparing
方法决定了顺序。
我们可以多次使用Comparator.thenComparing
。
当我们想通过排序键组来确定元素的顺序时,它很有用。
对于int
、long
和double
数据类型的排序键,比较器分别有thenComparingInt
、thenComparingLong
和thenComparingDouble
默认方法。
Comparator.thenComparing
thenComparing
有以下几种形式。
1.
default Comparator<T> thenComparing(Comparator<? super T> other)
它返回一个字典顺序比较器和另一个比较器。找到代码片段。
Comparator<Student> compByStdName = Comparator.comparing(Student::getName);
Comparator<Student> schoolComparator1 = Comparator.comparing(Student::getAge) //sort by student age
.thenComparing(compByStdName); //then sort by student name
首先,比较器将按照学生年龄对学生集合进行排序,如果某些学生的年龄相同,那么将按照他们的名字进行排序。
2.
default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor)
返回一个字典顺序比较器,其中包含一个提取Comparable
排序键的函数。找到代码片段。
Comparator<Student> schoolComparator2 = Comparator.comparing(Student::getSchool) //sort by school natural ordering i.e. city
.thenComparing(Student::getAge) //then sort by student age
.thenComparing(Student::getName); //then sort by student name
首先,学生的集合将按照他们各自的学校的自然顺序进行排序,如果一些学生根据他们的学校排序是相同的,那么这些学生将按照他们各自的年龄进行排序,如果年龄也是相同的,那么他们将按照他们的名字进行排序。
3.
default <U> Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)
它返回一个词表顺序的比较器,该比较器带有一个函数,可以提取一个键与给定的比较器进行比较。查找代码片段。
Comparator<Student> schoolComparator3 = Comparator.comparing(Student::getSchool) //sort by school natural ordering i.e. city
.thenComparing(Student::getSchool, (school1, school2) -> school1.getSname().compareTo(school2.getSname())) //then sort by school name
.thenComparing(Student::getAge) //then sort by student age
.thenComparing(Student::getName); //then sort by student name
首先,学生的集合将按他们各自的学校的自然顺序进行排序(即在我们的演示中按学校所在城市),然后如果学生在同一学校所在城市,他们将按各自的学校名称进行排序,如果学生在同一学校名称,他们将按年龄进行排序,如果学生在同一年龄,他们将按名字进行排序。
示例
School.java
public class School implements Comparable<School> {
private String sname;
private String city;
public School(String sname, String city) {
this.sname = sname;
this.city = city;
}
public String getSname() {
return sname;
}
public String getCity() {
return city;
}
@Override
public int compareTo(School s) {
return s.getCity().compareTo(city);
}
}
Student.java
import java.util.Arrays;
import java.util.List;
public class Student {
private String name;
private int age;
private long homeDistance;
private double weight;
private School school;
public Student(String name, int age, long homeDistance, double weight, School school) {
this.name = name;
this.age = age;
this.homeDistance = homeDistance;
this.weight = weight;
this.school = school;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public long getHomeDistance() {
return homeDistance;
}
public double getWeight() {
return weight;
}
public School getSchool() {
return school;
}
public static List<Student> getStudentList() {
Student s1 = new Student("Ram", 18, 3455, 60.75, new School("AB College", "Noida"));
Student s2 = new Student("Shyam", 22, 3252, 65.80, new School("RS College", "Gurugram"));
Student s3 = new Student("Mohan", 18, 1459, 65.20, new School("AB College", "Noida"));
Student s4 = new Student("Mahesh", 22, 4450, 70.25, new School("RS College", "Gurugram"));
List<Student> list = Arrays.asList(s1, s2, s3, s4);
return list;
}
}
ThenComparingDemo.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ThenComparingDemo {
public static void main(String[] args) {
List<Student> list = Student.getStudentList();
System.out.println("--------Example-1---------");
Comparator<Student> compByStdName = Comparator.comparing(Student::getName);
Comparator<Student> schoolComparator1 = Comparator.comparing(Student::getAge) //sort by student age
.thenComparing(compByStdName); //then sort by student name
Collections.sort(list, schoolComparator1);
list.forEach(s->System.out.println(s.getName() + "-" + s.getAge()));
System.out.println("--------Example-2---------");
Comparator<Student> schoolComparator2 = Comparator.comparing(Student::getSchool) //sort by school natural ordering i.e. city
.thenComparing(Student::getAge) //then sort by student age
.thenComparing(Student::getName); //then sort by student name
Collections.sort(list, schoolComparator2);
list.forEach(s->System.out.println(s.getName() + "-" + s.getAge()+ "-" + s.getSchool().getCity()));
System.out.println("--------Example-3---------");
Comparator<Student> schoolComparator3 = Comparator.comparing(Student::getSchool) //sort by school natural ordering i.e. city
.thenComparing(Student::getSchool, (school1, school2) -> school1.getSname().compareTo(school2.getSname())) //then sort by school name
.thenComparing(Student::getAge) //then sort by student age
.thenComparing(Student::getName); //then sort by student name
Collections.sort(list, schoolComparator3);
list.forEach(s->System.out.println(s.getName() + "-" + s.getAge()+ "-" + s.getSchool().getSname() + "-" + s.getSchool().getCity()));
}
}
输出
--------Example-1---------
Mohan-18
Ram-18
Mahesh-22
Shyam-22
--------Example-2---------
Mohan-18-Noida
Ram-18-Noida
Mahesh-22-Gurugram
Shyam-22-Gurugram
--------Example-3---------
Mohan-18-AB College-Noida
Ram-18-AB College-Noida
Mahesh-22-RS College-Gurugram
Shyam-22-RS College-Gurugram
Comparator.thenComparingInt
找到thenComparingInt
方法的声明。
default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor)
它返回一个字典顺序比较器,其中包含一个提取int
排序键的函数。找到例子。
ThenComparingIntDemo.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ThenComparingIntDemo {
public static void main(String[] args) {
List<Student> list = Student.getStudentList();
Comparator<Student> comparator = Comparator.comparing(Student::getName, (s1, s2) -> s1.charAt(0) - s2.charAt(0))
.thenComparingInt(Student::getAge);
Collections.sort(list, comparator);
list.forEach(s->System.out.println(s.getName() + "-" + s.getAge()));
}
}
输出
Mohan-18
Mahesh-22
Ram-18
Shyam-22
Comparator.thenComparingLong
找到thenComparingLong
方法的声明。
default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor)
它返回一个字典顺序比较器,其中包含一个提取long
排序键的函数。找到例子。
ThenComparingLongDemo.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ThenComparingLongDemo {
public static void main(String[] args) {
List<Student> list = Student.getStudentList();
Comparator<Student> comparator = Comparator.comparing(Student::getName, (s1, s2) -> s1.charAt(0) - s2.charAt(0))
.thenComparingLong(Student::getHomeDistance);
Collections.sort(list, comparator);
list.forEach(s->System.out.println(s.getName() + "-" + s.getHomeDistance()));
}
}
输出
Mohan-1459
Mahesh-4450
Ram-3455
Shyam-3252
Comparator.thenComparingDouble
找到thenComparingDouble
的方法声明。
default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor)
它返回一个字典顺序比较器,其中包含一个提取double
排序键的函数。找到例子。
ThenComparingDoubleDemo.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ThenComparingDoubleDemo {
public static void main(String[] args) {
List<Student> list = Student.getStudentList();
Comparator<Student> comparator = Comparator.comparing(Student::getName, (s1, s2) -> s1.charAt(0) - s2.charAt(0))
.thenComparingDouble(Student::getWeight);
Collections.sort(list, comparator);
list.forEach(s->System.out.println(s.getName() + "-" + s.getWeight()));
}
}
输出
Mohan-65.2
Mahesh-70.25
Ram-60.75
Shyam-65.8
参考文献
【1】Interface Comparator【2】Java Comparator.comparing【3】Java Comparator.thenComparing