Stream.Collect 收集器的使用案例

public static void main(String[] args) {
        
        Person p1 = new Person("zhangsan",20);
        Person p2 = new Person("lisi",30);
        Person p3 = new Person("wangwu",40);
        Person p4 = new Person("赵六",40);
        List<Person> list = Arrays.asList(p1, p2, p3, p4);

        System.out.println("获取流中所有元素");
        List<Person> collect = list.stream().collect(Collectors.toList());
        collect.forEach(System.out::println);

        //收集统计数据的对象 例如 最大最小平均总和  这个对象只返回数据 不返回对象 例如最小:返回20  而不是这个person
        IntSummaryStatistics intSummaryStatistics = list.stream()
            .collect(Collectors.summarizingInt(Person::getAge));

        System.out.println("-----------------------------");

        System.out.println("统计流中元素个数");
        System.out.println("方式一stream.count() : " + list.stream().count());
        System.out.println("方式二stream.collect(Collectors.counting()): " + list.stream().collect(Collectors.counting()));
        System.out.println("方式三:" + intSummaryStatistics.getCount());

        System.out.println("-------------------");

        System.out.println("获取流中  年龄最小的元素");
        System.out.print("获取最小方式一:");
        list.stream().collect(Collectors.minBy(Comparator.comparingInt(Person::getAge))).ifPresent(System.out::println);
        System.out.println("获取最小方式二:" + intSummaryStatistics.getMin());
        System.out.println("-------------------");

        System.out.println("获取流中  年龄最大的元素");
        System.out.print("获取最大方式一:");
        list.stream().collect(Collectors.maxBy(Comparator.comparingInt(Person::getAge))).ifPresent(System.out::println);
        System.out.println("获取最大方式二:" + intSummaryStatistics.getMax());

        System.out.println("-------------------");

        //年龄平均数
        Double average = list.stream().collect(Collectors.averagingInt(Person::getAge));
        System.out.println("平均年龄方式一:" + average);
        System.out.println("平均年龄方式二:" + intSummaryStatistics.getAverage());

        System.out.println("-------------------");

        System.out.println("年龄总和方式一:" + intSummaryStatistics.getSum());
        Integer sum = list.stream().collect(Collectors.summingInt(Person::getAge));
        System.out.println("年龄总和方式二:" + sum);

        System.out.println("-------------------");
        System.out.println("拼接所有人的性名");
        String personNames = list.stream().map(Person::getName).collect(Collectors.joining("  "));
        System.out.println("所有人的性名:" + personNames);

        System.out.println("-------------------");
        System.out.println("一级分组");
        System.out.println("根据年龄分组");
        Map<Integer, List<Person>> map = list.stream()
            .collect(Collectors.groupingBy(Person::getAge));
        System.out.println(map);

        System.out.println("二级分组  先根据年龄分组 在根据xing'm'z");
        Map<Integer, Map<String, List<Person>>> mapMap = list.stream()
            .collect(Collectors.groupingBy(Person::getAge, Collectors.groupingBy(Person::getName)));
        System.out.println(mapMap);

        System.out.println("-------------------");

        System.out.println("分区 <30 , >=30");
        Map<Boolean, List<Person>> listMap = list.stream()
            .collect(Collectors.partitioningBy(person -> person.getAge() < 30));
        System.out.println(listMap);

        System.out.println("-------------------");

        System.out.println("分区 <30 , >=30 后 >=30 这个分区再次分区  =30 和 >30");
        Map<Boolean, Map<Boolean, List<Person>>> map1 = list.stream().collect(Collectors
            .partitioningBy(person -> person.getAge() >= 30,
                Collectors.partitioningBy(person -> person.getAge() == 30)));
        System.out.println(map1);

        System.out.println("--------------");
        System.out.println("分区后统计数量");
        Map<Boolean, Long> map2 = list.stream().collect(
            Collectors.partitioningBy(person -> person.getAge() < 30, Collectors.counting()));
        System.out.println(map2);


    }

你可能感兴趣的:(#,java8新特性)