栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java Stream api 测试代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 mapper  处理T类型数据返回R类型数据


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


        //去重
        final Stream 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 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 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 

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号