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

内容简介

本文主要说明在Java8及以上版本中,使用stream().filter()来过滤一个List对象,查找符合条件的对象集合。

List对象类(StudentInfo)

public class StudentInfo implements Comparable<StudentInfo> {

    //名称
    private String name;

    //性别 true男 false女
    private Boolean gender;

    //年龄
    private Integer age;

    //身高
    private Double height;

    //出生日期
    private LocalDate birthday;

    public StudentInfo(String name, Boolean gender, Integer age, Double height, LocalDate birthday){
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.height = height;
        this.birthday = birthday;
    }

    public String toString(){
        String info = String.format("%s\t\t%s\t\t%s\t\t\t%s\t\t%s",this.name,this.gender.toString(),this.age.toString(),this.height.toString(),birthday.toString());
        return info;
    }

    public static void printStudents(List<StudentInfo> studentInfos){
        System.out.println("[姓名]\t\t[性别]\t\t[年龄]\t\t[身高]\t\t[生日]");
        System.out.println("----------------------------------------------------------");
        studentInfos.forEach(s->System.out.println(s.toString()));
        System.out.println(" ");
    }

    @Override
    public int compareTo(StudentInfo ob) {
        return this.age.compareTo(ob.getAge());
        //return 1;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getGender() {
        return gender;
    }

    public void setGender(Boolean gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getHeight() {
        return height;
    }

    public void setHeight(Double height) {
        this.height = height;
    }

    public LocalDate getBirthday() {
        return birthday;
    }

    public void setBirthday(LocalDate birthday) {
        this.birthday = birthday;
    }
}

测试数据

//测试数据,请不要纠结数据的严谨性
List<StudentInfo> studentList = new ArrayList<>();
studentList.add(new StudentInfo("李小明",true,18,1.76,LocalDate.of(2001,3,23)));
studentList.add(new StudentInfo("张小丽",false,18,1.61,LocalDate.of(2001,6,3)));
studentList.add(new StudentInfo("王大朋",true,19,1.82,LocalDate.of(2000,3,11)));
studentList.add(new StudentInfo("陈小跑",false,17,1.67,LocalDate.of(2002,10,18)));

输出Students列表

//输出List
StudentInfo.printStudents(studentList);

输出结果如下图:

image

使用filter()过滤List

//查找身高在1.8米及以上的男生
List<StudentInfo> boys = studentList.stream().filter(s->s.getGender() && s.getHeight() >= 1.8).collect(Collectors.toList());
//输出查找结果
StudentInfo.printStudents(boys);

结果如下图:

image

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
list.stream().filter方法用于对传入的数据流进行过滤处理,只返回满足条件的数据组成的新的数据流。在你提供的代码中,filter方法的参数是一个Lambda表达式(i->i>50),表示筛选出大于50的整数。返回的结果是一个新的List,其中包含满足条件的元素。所以在你的代码中,result列表中只包含大于50的元素,即[232]。 与filter方法类似,map方法也是对数据流进行处理的方法。它将传入的数据流中的每个元素映射成一个新的元素,并返回一个包含这些新元素的数据流。举个例子,如果你有一个整数列表[1, 2, 3],你可以使用map方法将每个元素都乘以2,得到一个新的列表[2, 4, 6]。 findFirst方法用于返回非空集合中的第一个满足条件的元素值。它通常与filter方法结合使用。在你提供的代码中,findFirst方法的参数是一个Lambda表达式(i->i>20),表示找到大于20的第一个元素。如果找到了满足条件的元素,则返回Optional类型的结果,你可以使用op.get()获取该元素的值。如果没有找到满足条件的元素,则返回Optional.empty()。 findAny方法与findFirst方法类似,它也是用于在数据流中查找满足条件的元素。不同之处在于,findAny方法可以在并行处理的流中找到任意一个满足条件的元素,并返回该元素的值。所以在你提供的代码中,parallelStream方法将列表转换为并行处理的流,然后使用filter方法筛选出大于20的元素,并使用findAny方法返回第一个满足条件的元素,最后使用get方法获取该元素的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值