集合排序(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);
}
}
如果我想根据他们的名字和学生排序学生列表,请为本课程。年龄如何使用方法集合排序(列表,比较器)
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)
推荐答案
以现有学生为基础 class,这就是我通常这样做的方式,特别是如果我需要多个比较器的话。
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发布以来,使用lambdas可以大大简化内部类 Comparators
。 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);
}
这篇关于集合排序(List< T>,Comparator<?super T>)方法示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!