Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)

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);

输出结果:

姓名性别年龄身高生日
贺小群true181.762020-03-23
贺大群false181.612020-06-03
贺群true191.822020-03-11
贺群群false171.672020-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);

结果如下:

姓名性别年龄身高生日
贺群true191.822000-03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值