JAVA8 Stram流式操作 案例

本文展示了如何使用JavaStreamAPI进行数据处理,包括创建流、终止操作如查找第一个元素、任何元素、匹配条件、归集、统计信息等,以及中间操作如过滤、映射、排序、分组和peek操作。代码示例详细解释了各种操作的用法。
摘要由CSDN通过智能技术生成

创建stream
终止操作
中间操作

List<Person> personList = new ArrayList<Person>();
    List<Integer> simpleList = Arrays.asList(15, 22, 9, 11, 33, 52, 14);

    @Before
    public void initData(){
        personList.add(new Person("张三",3000,23,"男","太原"));
        personList.add(new Person("李四",7000,34,"男","西安"));
        personList.add(new Person("王五",5200,22,"女","太原"));
        personList.add(new Person("小黑",1500,33,"女","上海"));
        personList.add(new Person("狗子",8000,44,"女","北京"));
        personList.add(new Person("铁蛋",6200,36,"女","南京"));
    }
    @Test
    public void terminateTest(){//foreach find match

        simpleList.stream().forEach(System.out::println);
        Optional<Integer> first = simpleList.stream().findFirst();
        Optional<Integer> any = simpleList.parallelStream().findAny();
        System.out.println(first +"   "+any);
        boolean b = simpleList.stream().allMatch(x -> x > 30);

    }
    @Test
    public void terminateTest2(){//归集(toList/toSet/toMap)
        List<Person> collect = personList.stream().collect(toList());
        Set<Person> collect2 = personList.stream().collect(Collectors.toSet());
        Map<Integer,Integer> collect3 = simpleList.stream().collect(Collectors.toMap(t->t,t->t+1));

        System.out.println(collect3);
    }
    @Test
    public void terminateTest3(){// 统计(count/averaging/sum/max/min)
        long count = personList.stream().count();
        System.out.println("总人数:"+count);

        Double collect = personList.stream().collect(Collectors.averagingInt(Person::getSalary));
        System.out.println("平均薪资 "+collect);

        //summarizing 将数据分装了 直接获取即可!
        IntSummaryStatistics collect1 = personList.stream().collect(Collectors.summarizingInt(Person::getSalary));

        System.out.println("员工总薪资"+collect1);
        Optional<Person> max = personList.stream().max((p1, p2) -> p1.getSalary() - p2.getSalary());
        Optional<Person> max2 = personList.stream().max(Comparator.comparing(Person::getSalary));
        System.out.println("最高薪资"+max+" "+max2);


    }
    @Test
    public void terminateTest4(){//归约(reduce)
        Integer reduce = simpleList.stream().reduce(1, (x, y) -> x * y);
        System.out.println(reduce);//
    }
    @Test
    public void terminateTest5(){//接合(joining)
        String collect = Arrays.asList("a", "b", "cde").stream().collect(Collectors.joining("---"));
        System.out.println(collect);
    }
    @Test
    public void terminateTest6(){//分组(partitioningBy/groupingBy)
        Map<Boolean, List<Person>> collect = personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 6000));
        Map<String, List<Person>> collect1 = personList.stream().collect(Collectors.groupingBy(Person::getSex));
        Map<String, Map<String, List<Person>>> group2 = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
        System.out.println(collect);
        System.out.println(collect1);
        System.out.println(group2);

    }
    @Test
    public void intermediate_operations1(){//筛选(filter)
        Stream<Integer> integerStream = simpleList.stream().filter(x -> x > 30);
        integerStream.forEach(System.out::println);
    }
    @Test
    public void intermediate_operations2(){//映射(map/flatMap)

        System.out.println("前;"+personList.stream().map(x->x.getSalary()).collect(Collectors.toList()));
        List<Person> collect = personList.stream().map(x -> {
            x.setSalary(x.getSalary() + 100);
            return x;
        }).collect(toList());
        System.out.println("后:"+collect.stream().map(x->x.getSalary()).collect(Collectors.toList()));

        String[] words = new String[]{"Hello","World"};
        List<String> a = Arrays.stream(words)
                .map(word -> word.split(""))
                .flatMap(Arrays::stream)
                .distinct()
                .collect(toList());
        a.forEach(System.out::print);

    }
    @Test
    public void intermediate_operations3(){//排序(sorted)
        // 按工资升序排序(自然排序)
        List<String> newList = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName)
                .collect(Collectors.toList());
        // 按工资倒序排序
        List<String> newList2 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed())
                .map(Person::getName).collect(Collectors.toList());
        // 先按工资再按年龄升序排序
        List<String> newList3 = personList.stream()
                .sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).map(Person::getName)
                .collect(Collectors.toList());
        // 先按工资再按年龄自定义排序(降序)
        List<String> newList4 = personList.stream().sorted((p1, p2) -> {
            if (p1.getSalary() == p2.getSalary()) {
                return p2.getAge() - p1.getAge();
            } else {
                return p2.getSalary() - p1.getSalary();
            }
        }).map(Person::getName).collect(Collectors.toList());

        System.out.println("按工资升序排序:" + newList);
        System.out.println("按工资降序排序:" + newList2);
        System.out.println("先按工资再按年龄升序排序:" + newList3);
        System.out.println("先按工资再按年龄自定义降序排序:" + newList4);
    }
    @Test
    public void intermediate_operations4(){//peek操作
        // 在stream中间进行调试,因为stream不支持debug
        List<Person> collect = personList.stream().filter(p -> p.getSalary() > 5000)
                .peek(System.out::println).collect(Collectors.toList());
        // 修改元素的信息,给每个员工涨工资一千
        //personList.stream().peek(p -> p.setSalary(p.getSalary() + 1000))
        //.forEach(System.out::println);
    }
    @Test
    public void intermediate_operations5() {
    /*其他操作
        流也可以进行合并、去重、限制、跳过等操作。*/
        simpleList.stream().distinct().skip(2).limit(3).forEach(System.out::println);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值