集合 sort(List<T>,Comparator<? super T>) 方法示例 [英] Collections sort(List<T>,Comparator<? super T>) method example
问题描述
可能的重复:
使用多个键对 Java 对象进行排序
我找不到任何使用此方法的示例,所有示例都给出了第二个参数null".我听说这种方法用于根据多个标准对类进行排序,但没有找到示例.
I can't find any example of using this method, all examples give the second parameter "null". I heard that this method used for sorting classes according to more than one criterion but no example where found.
public class Student implements Comparable<Student> {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name + ":" + age;
}
@Override
public int compareTo(Student o) {
Integer myAge = age;
Integer oAge = o.age;
return myAge.compareTo(oAge);
}
}
对于这门课,如果我想根据学生的姓名对学生列表进行排序 &我该如何使用 Collections sort(List,Comparator)
for this class if i want to sort a list of Student according to their names & ages how can i use the method Collections sort(List,Comparator)
推荐答案
以您现有的Student 类为基础,这就是我通常的做法,尤其是当我需要多个比较器时.
Building upon your existing Student class, this is how I usually do it, especially if I need more than one comparator.
public class Student implements Comparable<Student> {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name + ":" + age;
}
@Override
public int compareTo(Student o) {
return Comparators.NAME.compare(this, o);
}
public static class Comparators {
public static Comparator<Student> NAME = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.name.compareTo(o2.name);
}
};
public static Comparator<Student> AGE = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.age - o2.age;
}
};
public static Comparator<Student> NAMEANDAGE = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int i = o1.name.compareTo(o2.name);
if (i == 0) {
i = o1.age - o2.age;
}
return i;
}
};
}
}
用法:
List<Student> studentList = new LinkedList<>();
Collections.sort(studentList, Student.Comparators.AGE);
编辑
自 Java 8 发布以来,内部类 Comparator
可以使用 lambda 大大简化.Java 8 还为 Comparator
对象 thenComparing
引入了一个新方法,它消除了在嵌套每个比较器时手动检查它们的需要.下面是考虑了这些更改的 Student.Comparators
类的 Java 8 实现.
Since the release of Java 8 the inner class Comparators
may be greatly simplified using lambdas. Java 8 also introduces a new method for the Comparator
object thenComparing
, which removes the need for doing manual checking of each comparator when nesting them. Below is the Java 8 implementation of the Student.Comparators
class with these changes taken into account.
public static class Comparators {
public static final Comparator<Student> NAME = (Student o1, Student o2) -> o1.name.compareTo(o2.name);
public static final Comparator<Student> AGE = (Student o1, Student o2) -> Integer.compare(o1.age, o2.age);
public static final Comparator<Student> NAMEANDAGE = (Student o1, Student o2) -> NAME.thenComparing(AGE).compare(o1, o2);
}
这篇关于集合 sort(List<T>,Comparator<? super T>) 方法示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!