Java 8 stream 的使用

        Student student1 = new Student("zhangsan",12);
        Student student2 = new Student("lisi",13);
        Student student3 = new Student("wangwu",11);
        Student student4 = new Student("sunliu",14);
        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);

        System.out.println(list);
        //[Student{name='zhangsan', age=12}, Student{name='lisi', age=13}, Student{name='wangwu', age=11}, Student{name='sunliu', age=14}]

        Stream<Student> stream = list.stream();//将集合转化为流
        System.out.println(stream);
        //java.util.stream.ReferencePipeline$Head@7d4793a8

        //使用collect 降流转化为集合
        List<Student> studentList = list.stream().collect(Collectors.toList());
        System.out.println(studentList);
        //[Student{name='zhangsan', age=12}, Student{name='lisi', age=13}, Student{name='wangwu', age=11}, Student{name='sunliu', age=14}]

       //将转化为流的集合过滤出满足条件的流
        List<Student> students = list.stream().filter(student -> student.getAge() > 11) //筛选年龄大于11的学生
                               .collect(Collectors.toList());
        System.out.println(students);

        //将每个元素映射成新的元素
        List<Student> collect = list.stream().filter(student -> student.getAge() > 12) //先将学生年龄过滤掉12
                .map(student -> {
                    student.setAge(19); //将学生设置为19 并返回
                    return student;
                }).collect(Collectors.toList());
        System.out.println(collect);
        //[Student{name='lisi', age=19}, Student{name='sunliu', age=19}]

        //获取第n个元素
        List<Student> collect1 = list.stream().limit(2).collect(Collectors.toList()); //获取前2个元素
        System.out.println(collect1);
        //[Student{name='zhangsan', age=12}, Student{name='lisi', age=19}]

        //跳过N个元素
        List<Student> collect2 = list.stream().skip(2).collect(Collectors.toList());//跳过前2个元素获取剩下的
        System.out.println(collect2);
        //[Student{name='wangwu', age=11}, Student{name='sunliu', age=19}]

        //limit 和 skip 结合使用相当于分页
        List<Student> collect3 = list.stream().skip(2).limit(1).collect(Collectors.toList());
        System.out.println(collect3);  //跳过去前俩个元素 ,获取剩下的一个元素
        //[Student{name='wangwu', age=11}]

        //去重
        Student student5 = new Student("zhangsan",12);
        //list.add(student5);
        List<Student> collect4 = list.stream().distinct().collect(Collectors.toList());
        System.out.println(collect4);
       //[Student{name='zhangsan', age=12}, Student{name='lisi', age=19}, Student{name='wangwu', age=11}, Student{name='sunliu', age=19}]

        //根据年龄升序
        List<Student> collect5 = list.stream().sorted(Comparator.comparing(student -> student.getAge())).collect(Collectors.toList());
        System.out.println(collect5);
       //[Student{name='wangwu', age=11}, Student{name='zhangsan', age=12}, Student{name='zhangsan', age=12}, Student{name='lisi', age=19}, Student{name='sunliu', age=19}]
       //降序
        List<Student> collect6 = list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
        System.out.println(collect6);
        //[Student{name='lisi', age=19}, Student{name='sunliu', age=19}, Student{name='zhangsan', age=12}, Student{name='zhangsan', age=12}, Student{name='wangwu', age=11}]

        //返回流中任意元素
        Optional<Student> any = list.stream().findAny();
        System.out.println(any);
      //Optional[Student{name='zhangsan', age=12}]`在这里插入代码片`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值