Java Comparator.comparing | 提取对象属性,按照指定规则排序
comparing
是比较器功能接口的静态方法。
Comparator.comparing
方法在Java 8
中被引入。
Comparator.comparing
接受一个函数,该函数从给定类型中提取一个可比较的排序键,并返回一个通过该排序键进行比较的比较器。
Comparator.comparing
有两种形式。
1.
static <T,U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor)
我们需要传递一个函数,它将从一个类型T
中提取一个可比较的排序键,并返回一个通过该排序键进行比较的比较器。
查找示例代码。
Comparator<Student> nameComparator = Comparator.comparing(Student::getName);
2.
static <T,U> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)
我们需要传递一个函数(Function
)和一个比较器(Comparator
)。该方法将从一个类型T
中提取一个排序键,并返回一个比较器,使用指定的比较器对该排序键进行比较。
查找示例代码。
Comparator<Student> nameComparator = Comparator.comparing(Student::getName, (s1, s2) -> s2.compareTo(s1));
对于int
、long
和double
数据类型的排序键,比较器分别有comparingInt
、comparingLong
和comparingDouble
方法。
Comparator.comparing
这里我们将提供Comparator.comparing
方法的例子。
找到一个将实现Comparable
接口的类,并定义compareTo
方法。
School.java
public class School implements Comparable<School> {
private int code;
private String sname;
public School(int code, String sname) {
this.code = code;
this.sname = sname;
}
public int getCode() {
return code;
}
public String getSname() {
return sname;
}
@Override
public int compareTo(School s) {
return s.sname.compareTo(sname);
}
}
创建另一个类,我们将在其中创建School
类。
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(101, "PQ College"));
Student s2 = new Student("Shyam", 22, 3252, 65.80, new School(103, "RS College"));
Student s3 = new Student("Mohan", 19, 1459, 65.20, new School(102, "AB College"));
Student s4 = new Student("Mahesh", 20, 4450, 70.25, new School(104, "CD College"));
List<Student> list = Arrays.asList(s1, s2, s3, s4);
return list;
}
}
现在找到Comparator.comparing
方法的例子。
ComparingDemo.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparingDemo {
public static void main(String[] args) {
List<Student> list = Student.getStudentList();
Comparator<Student> schoolComparator1 = Comparator.comparing(Student::getSchool);
Collections.sort(list, schoolComparator1);
list.forEach(s->System.out.print(s.getName() + "-" + s.getSchool().getSname() + " | "));
System.out.println("\n-------------------");
Comparator<Student> schoolComparator2 =
Comparator.comparing(Student::getSchool, (sch1, sch2) -> sch1.getCode() - sch2.getCode());
Collections.sort(list, schoolComparator2);
list.forEach(s->System.out.print(s.getName() + "-" + s.getSchool().getCode() + " | "));
System.out.println("\n-------------------");
Comparator<Student> nameComparator1 = Comparator.comparing(Student::getName);
Collections.sort(list, nameComparator1);
list.forEach(s->System.out.print(s.getName() + " "));
System.out.println("\n-------------------");
Comparator<Student> nameComparator2 = Comparator.comparing(Student::getName, (s1, s2) -> s2.compareTo(s1));
Collections.sort(list, nameComparator2);
list.forEach(s->System.out.print(s.getName() + " "));
}
}
输出
Shyam-RS College | Ram-PQ College | Mahesh-CD College | Mohan-AB College |
-------------------
Ram-101 | Mohan-102 | Shyam-103 | Mahesh-104 |
-------------------
Mahesh Mohan Ram Shyam
-------------------
Shyam Ram Mohan Mahesh
在上面的例子中,我们使用Collections.sort
对集合进行排序。我们也可以使用Stream.sorted
、List.sort
和Arrays.sort
来使用比较器对集合进行排序。
Comparator.comparingInt
comparingInt
方法声明:
static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor)
它接受一个从类型T
中提取一个int
排序键的函数,并返回一个通过该排序键进行比较的比较器。
找到这个例子。
ComparingIntDemo.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparingIntDemo {
public static void main(String[] args) {
List<Student> list = Student.getStudentList();
Collections.sort(list, Comparator.comparingInt(Student::getAge));
list.forEach(s->System.out.print(s.getAge() + " "));
}
}
输出
18 19 20 22
Comparator.comparingLong
comparingLong
方法声明:
static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor)
它接受一个从类型T
中提取一个long
排序键的函数,并返回一个通过该排序键进行比较的比较器。
ComparingLongDemo.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparingLongDemo {
public static void main(String[] args) {
List<Student> list = Student.getStudentList();
Collections.sort(list, Comparator.comparingLong(Student::getHomeDistance));
list.forEach(s->System.out.print(s.getHomeDistance() + " "));
}
}
输出
1459 3252 3455 4450
Comparator.comparingDouble
comparingDouble
方法声明:
static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor)
它接受一个从类型T
中提取一个double
排序键的函数,并返回一个通过该排序键进行比较的比较器。
ComparingDoubleDemo.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparingDoubleDemo {
public static void main(String[] args) {
List<Student> list = Student.getStudentList();
Collections.sort(list, Comparator.comparingDouble(Student::getWeight));
list.forEach(s->System.out.print(s.getWeight() + " "));
}
}
输出
60.75 65.2 65.8 70.25