集合 sort(List<T>,Comparator<? super T>) 方法示例

编程入门 行业动态 更新时间:2024-10-28 14:24:24
本文介绍了集合 sort(List<T>,Comparator<? super T>) 方法示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能的重复:使用多个键对 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 myAgepareTo(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.NAMEpare(this, o); } public static class Comparators { public static Comparator<Student> NAME = new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.namepareTo(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.namepareTo(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.namepareTo(o2.name); public static final Comparator<Student> AGE = (Student o1, Student o2) -> Integerpare(o1.age, o2.age); public static final Comparator<Student> NAMEANDAGE = (Student o1, Student o2) -> NAME.thenComparing(AGE)pare(o1, o2); }

更多推荐

集合 sort(List<T>,Comparator<? super T>) 方法示例

本文发布于:2023-11-08 16:35:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569900.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   方法   lt   List   sort

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!