java Stream api 测试代码

public static void main(String[] args) {
        Person[] person= new Person[]{new Person("张三",11),new Person("李四",12)};

        //创建方法
        //Stream.of(person);
        //集合stream

        //Intermediate 操作
        // map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered
        //Function<? super T, ? extends R> mapper  处理T类型数据返回R类型数据


        //依旧返回一个stream 筛选
        List<Person> collect2 = Stream.of(person).filter(p -> p.getAge() > 0).collect(Collectors.toList());


        //去重
        final Stream<Person> distinct = Stream.of(person).distinct();


        //排序
        Stream.of(person).sorted(Person::compareTo).collect(Collectors.toList());


        //这部操作会直接改变 person 的值
        System.out.println(Stream.of(person).peek(p-> p.setAge(90)).collect(Collectors.toList()));

        //并行的处理
        Stream.of(person).parallel();

        //串行
        Stream.of(person).sequential();

        List<Integer> collect = Stream.of(person).map(Person::initAge).collect(Collectors.toList());
        //Terminal 操作
        // forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator
        //循环输出
        Stream.of(person).forEach(System.out::println);
        //排序加输出
        Stream.of(person).forEachOrdered(System.out::println);

        Object[] objects = Stream.of(person).toArray();
        System.out.println(Arrays.toString(objects));

        //直接指定了数据类型
        Person[] people = Stream.of(person).toArray(Person[]::new);
        System.out.println(Arrays.toString(people));


        //归纳 参数为BinaryOperator 方法定义  (acc,item) acc 为上次计算后的结果 item 是当前条目 最后返回的是Optional
        //参数为BinaryOperator 继承 BiFunction 方法定义为传入两个参数 返回一个结果
        //可以设置初始值
        Integer init =20;
        Integer red = Stream.of(person).map(Person::getAge).reduce(init, (acc, item) -> {
            //做运算的
            acc += item;
            return acc;
        });
        System.out.println(red);

        //创建 累加   合并
        LinkedList<Object> collect1 = Stream.of(person).collect(LinkedList::new, LinkedList::add, LinkedList::addAll);
        System.out.println(collect1);

        //person要实现Comparable 接口
        Stream.of(person).min(Person::compareTo).ifPresent(System.out::println);
        Stream.of(person).max(Person::compareTo).ifPresent(System.out::println);
        long count = Stream.of(person).count();
        System.out.println(count);

        //有一个元素匹配返回true
        boolean b = Stream.of(person).anyMatch(p -> p.getAge() == 11);
        System.out.println(b);
        //要求所有元素都匹配
        System.out.println(Stream.of(person).allMatch(p -> p.getAge() < 30));

        //没有任何匹配返回true
        System.out.println(Stream.of(person).noneMatch(p -> p.getAge() > 30));

        //查找第一和元素 返回 Optional
        Stream.of(person).findFirst().ifPresent(System.out::println);

        //发现第一个元素就返回 //无返回会有异常
        System.out.println(Stream.of(person).findAny().orElse(null));

        //hort-circuiting 操作
    // anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 limit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值