stream — 归约与收集

Posted zhanh247

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了stream — 归约与收集相关的知识,希望对你有一定的参考价值。

1、reduce(T identity,Binaryoperator)/reduce(Binaryoperator)一可以将流中元素反复结合起来,得到一个值。

    List<Employee> employees = Arrays.asList(//
            new Employee(20, "张三", 5000.35, Status.FREE), //
            new Employee(40, "李四", 6500.63, Status.BUSY), //
            new Employee(30, "王五", 4000.93, Status.FREE), //
            new Employee(50, "赵六", 9005.36, Status.BUSY), //
            new Employee(10, "马七", 1050.93, Status.VOCATION), //
            new Employee(20, "朱八", 3000.73, Status.BUSY)//
    );

    @Test
    public void test1() {
        List<Integer> asList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        Integer reduce = asList.stream().reduce(0, (x, y) -> x + y);
        System.out.println(reduce);

        System.out.println("====================");
        Optional<Double> reduce2 = employees.stream().map(Employee::getSalary).reduce(Double::sum);
        System.out.println(reduce2.get());
    }

2、收集co1lect—将流转换为其他形式。接收一个Co1lector接口的实现,用于给Stream中元素做汇总的方法

    @Test
    public void test2() {
        employees.stream().map(Employee::getName).collect(Collectors.toList()).forEach(System.out::println);

        System.out.println("====================");
        employees.stream().map(Employee::getName).collect(Collectors.toSet()).forEach(System.out::println);

        System.out.println("====================");
        employees.stream().map(Employee::getName).collect(Collectors.toCollection(HashSet::new))
                .forEach(System.out::println);

    }

3、统计

    @Test
    public void test3() {

        // 数量
        Long collect = employees.stream().collect(Collectors.counting());
        System.out.println(collect);

        // 平均值
        System.out.println("====================");
        Double collect2 = employees.stream().collect(Collectors.averagingDouble(Employee::getSalary));
        System.out.println(collect2);

        // 总和
        System.out.println("====================");
        Double collect3 = employees.stream().collect(Collectors.summingDouble(Employee::getSalary));
        System.out.println(collect3);

        // 最大值
        System.out.println("====================");
        Optional<Employee> collect4 = employees.stream()
                .collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())));
        System.out.println(collect4.get());

        // 最小值
        System.out.println("====================");
        Optional<Double> collect5 = employees.stream().map(Employee::getSalary)
                .collect(Collectors.minBy(Double::compare));
        System.out.println(collect5.get());

    }

4、分组

    @Test
    public void test4() {
        Map<Status, List<Employee>> collect = employees.stream().collect(Collectors.groupingBy(Employee::getStatus));
        System.out.println(collect);

        System.out.println("====================");
        // 多级分组
        Map<Status, Map<String, List<Employee>>> collect2 = employees.stream()
                .collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy((e) -> {
                    if (((Employee) e).getAge() <= 35) {
                        return "青年";
                    } else if (((Employee) e).getAge() <= 50) {
                        return "中年";
                    } else {
                        return "老年";
                    }

                })));
        System.out.println(collect2);

    }

5、分区partitioningBy

    @Test
    public void test5() {
        Map<Boolean, List<Employee>> collect = employees.stream()
                .collect(Collectors.partitioningBy((e) -> e.getSalary() > 5000));
        System.out.println(collect);
    }

6、summarizingDouble

    @Test
    public void test6() {
        DoubleSummaryStatistics collect = employees.stream().collect(Collectors.summarizingDouble(Employee::getSalary));
        System.out.println(collect.getCount());
        System.out.println(collect.getAverage());
        System.out.println(collect.getMax());
        System.out.println(collect.getMin());
    }

7、joining

    @Test
    public void test7() {
        String collect = employees.stream().map(Employee::getName).collect(Collectors.joining(",", "====", "==="));
        System.out.println(collect);
    }

 

以上是关于stream — 归约与收集的主要内容,如果未能解决你的问题,请参考以下文章

Java8新特性——Stream API的终止操作

Java8新特性——Stream API的终止操作

Java 8 Stream 流 - 收集数据

玩转Java8的 Stream 之Collectors收集器

《Java8实战》 - 读书笔记 - Stream流操作2:用流收集数据

jdk1.8新特性——Stream(流)的终止操作(归约的示例演示)