Java中如何用lambda表达式来排序
作者:WeekendFlower
时间:2023-05-01
来源:互联网
浏览:0
1.lambda表达式排序我们首先看几个比较常见的排序例子,基本数据类型的排序Listlist=Arrays.asList(1,3,2,5,4);list.sort(Comparator.naturalOrder());System.out.println(list);list.sort(Comparator.reverseOrder());System.out.println(list);输出结果:[1,2,3,4,5][5,4,3,2,1]我们可以看到执行结果是符合预期的,但是多数场景我们可能需要针对
1.lambda表达式排序
我们首先看几个比较常见的排序例子,基本数据类型的排序
List list = Arrays.asList(1,3,2,5,4); list.sort(Comparator.naturalOrder()); System.out.println(list); list.sort(Comparator.reverseOrder()); System.out.println(list); 输出结果: [1, 2, 3, 4, 5] [5, 4, 3, 2, 1]
我们可以看到执行结果是符合预期的,但是多数场景我们可能需要针对对象的某个属性进行排序,那么应该怎样做呢?我们看下边的例子:
public class Student {
private String name;
private String sexual;
private Integer age;
public Student(String name, String sexual,Integer age) {
this.name = name;
this.sexual = sexual;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSexual() {
return sexual;
}
public void setSexual(String sexual) {
this.sexual = sexual;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sexual='" + sexual + '\'' +
", age=" + age +
'}';
}
public class Starter {
public static void main(String[] args) {
List list = Arrays.asList(
new Student("jack", 12),
new Student("john", 13),
new Student("lily", 11),
new Student("lucy", 10)
);
list.sort(Comparator.comparing(Student::getAge));
System.out.println(list);
list.sort(Comparator.comparing(Student::getAge).reversed());
System.out.println(list);
}
}
输出结果:
[Student{name='lucy', age=10}, Student{name='lily', age=11}, Student{name='jack', age=12}, Student{name='john', age=13}]
[Student{name='john', age=13}, Student{name='jack', age=12}, Student{name='lily', age=11}, Student{name='lucy', age=10}] 如果我们需要按照性别分组再排序又该如何实现呢?我们接着看下边的例子
public class Starter {
public static void main(String[] args) {
List list = Arrays.asList(
new Student("jack", "male", 12),
new Student("john", "male", 13),
new Student("lily", "female", 11),
new Student("david", "male", 14),
new Student("luck", "female", 13),
new Student("jones", "female", 15),
new Student("han", "male", 13),
new Student("alice", "female", 11),
new Student("li", "male", 12)
);
Map> groupMap = list.stream().sorted(Comparator.comparing(Student::getAge))
.collect(Collectors.groupingBy(Student::getSexual, Collectors.toList()));
System.out.println(groupMap.toString());
}
}
输出结果:
{
female = [
Student {
name = 'lily', sexual = 'female', age = 11
},
Student {
name = 'alice', sexual = 'female', age = 11
}, Student {
name = 'luck', sexual = 'female', age = 13
}, Student {
name = 'jones', sexual = 'female', age = 15
}],
male = [
Student {
name = 'jack', sexual = 'male', age = 12
}, Student {
name = 'li', sexual = 'male', age = 12
}, Student {
name = 'john', sexual = 'male', age = 13
}, Student {
name = 'han', sexual = 'male', age = 13
}, Student {
name = 'david', sexual = 'male', age = 14
}]
} 我们看到上边的输出结果存在一个问题,如果年龄相同则没有按照姓名排序,怎样实现这个功能呢?我们接着看下边的例子
Map> groupMap = list.stream().sorted(Comparator.comparing(Student::getAge) .thenComparing(Student::getName)).collect(Collectors.groupingBy(Student::getSexual, Collectors.toList())); 输出结果: { female = [ Student { name = 'alice', sexual = 'female', age = 11 }, Student { name = 'lily', sexual = 'female', age = 11 }, Student { name = 'luck', sexual = 'female', age = 13 }, Student { name = 'jones', sexual = 'female', age = 15 }], male = [ Student { name = 'jack', sexual = 'male', age = 12 }, Student { name = 'li', sexual = 'male', age = 12 }, Student { name = 'han', sexual = 'male', age = 13 }, Student { name = 'john', sexual = 'male', age = 13 }, Student { name = 'david', sexual = 'male', age = 14 }] }
作者最新文章
赤友清理大师
2026-09-16 17:43
南邮光擎智算团队:GaN基Micro-LED光计算芯片从理论到流片的突破
2026-09-08 18:35
多张照片怎么合成PDF文件?三种图片转PDF工具怎么选?
2026-09-03 17:04
Excel转PDF防乱版指南:在线与本地双方案及排版检查
2026-09-03 10:04
多个PDF怎么合并成一个?合并后顺序怎么检查?
2026-09-02 19:54
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















