StudentInfo对象类
@Data
public class StudentInfo {
//名称
private String name;
//性别 true男 false女
private Boolean gender;
//年龄
private Integer age;
//身高
private Double height;
//出生日期
private LocalDate birthday;
}
测试数据
List<StudentInfo> studentList = new ArrayList<>();
studentList.add(new StudentInfo("贺小群",true,18,1.76,LocalDate.of(2020,3,23)));
studentList.add(new StudentInfo("贺大群",false,18,1.61,LocalDate.of(2020,6,3)));
studentList.add(new StudentInfo("贺群",true,19,1.82,LocalDate.of(2020,3,11)));
studentList.add(new StudentInfo("贺群群",false,17,1.67,LocalDate.of(2020,10,18)));
输出studentList列表
//输出List
StudentInfo.printStudents(studentList);
输出结果:
| 姓名 | 性别 | 年龄 | 身高 | 生日 |
|---|---|---|---|---|
| 贺小群 | true | 18 | 1.76 | 2020-03-23 |
| 贺大群 | false | 18 | 1.61 | 2020-06-03 |
| 贺群 | true | 19 | 1.82 | 2020-03-11 |
| 贺群群 | false | 17 | 1.67 | 2020-10-18 |
1.使用filter()过滤List
//查找身高在1.8米及以上的男生
List<StudentInfo> boys = studentList.stream().filter(s->s.getGender() && s.getHeight() >= 1.8).collect(Collectors.toList());
//输出查找结果
StudentInfo.printStudents(boys);
结果如下:
| 姓名 | 性别 | 年龄 | 身高 | 生日 |
|---|---|---|---|---|
| 贺群 | true | 19 | 1.82 | 2000-03-11 |
Java后端筛选与输出:基于StudentInfo对象的身高筛选与打印
8378

被折叠的 条评论
为什么被折叠?



