lambda函数——Collect使用(六)

    /**
     * collect 收集的目的就是为了下一步的转换准备的
     *     案例一  :收集某个属性到集合中
     */
    @Test
    public void test(){
        List<Man> integers = Arrays.asList(
                new Man(1,"1",1.0),
                new Man(2,"2",2.0),
                new Man(3,"3",3.0)
        );

        integers.stream()
                .map(Man::getName)
                .collect(Collectors.toList())
                .forEach(System.out::println);
    }


    /**
     * 收集去重
     */
    @Test
    public void test2(){
        List<Man> integers = Arrays.asList(
                new Man(1,"2",1.0),
                new Man(2,"2",2.0),
                new Man(3,"3",3.0)
        );
        integers.stream()
                .map(Man::getName)
                .collect(Collectors.toSet())
                .forEach(System.out::println);

        //收集到指定的容器中
        integers.stream()
                .map(Man::getName)
                .collect(Collectors.toCollection(HashSet::new))
                .forEach(System.out::println);
        //收集个数
        Long collect = integers.stream()
                .collect(Collectors.counting());
        System.out.println(collect);
        //收集平均值
        Double collect1 = integers.stream()
                .collect(Collectors.averagingDouble(Man::getAge));
        System.out.println(collect1);


        DoubleSummaryStatistics collect2 = integers.stream()
                .collect(Collectors.summarizingDouble(Man::getAge));
        System.out.println("sum"+collect2);
        System.out.println("avg"+collect2.getAverage());
        System.out.println("max"+collect2.getMax());
        System.out.println("min"+collect2.getMin());
        System.out.println("count"+collect2.getCount());

    }

    /**
     * 分组进行数据操作
     */
    @Test
    public void group(){
        List<Man2> integers = Arrays.asList(
                new Man2(1,"2","男",1.0),
                new Man2(2,"2","男",2.0),
                new Man2(3,"3","女",3.0)
        );
        Map<String, List<Man2>> collect = integers.stream().collect(Collectors.groupingBy(Man2::getSex));
        Iterator<Map.Entry<String, List<Man2>>> iterator = collect.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, List<Man2>> next = iterator.next();
            System.out.println(next.getKey() + "--" +next.getValue());
        }
    }

    /**
     * 多个属性分组
     */
    @Test
    public void test5(){
        List<Man2> integers = Arrays.asList(
                new Man2(1,"2","男",1.0),
                new Man2(2,"2","男",2.0),
                new Man2(3,"3","女",3.0)
        );
        Map<String, Map<Double, List<Man2>>> collect = integers.stream().
                collect(Collectors.groupingBy(Man2::getSex, Collectors.groupingBy(Man2::getAge)));
        System.out.println(collect);

    }

    /**
     * 按照要求返回格式
     */
    @Test
    public void test6(){
        List<Man2> integers = Arrays.asList(
                new Man2(1,"2","男",1.0),
                new Man2(2,"2","男",2.0),
                new Man2(3,"3","女",3.0)
        );
        Map<String, Map<Object, List<Man2>>> collect = integers.stream().
                collect(Collectors.groupingBy(Man2::getSex,
                        Collectors.groupingBy(x -> {
                            return "还可以添加自定义判断";
                        })));
        System.out.println(collect);

    }


    // 拼接字符串,把实体中某属性进行拼接
    @Test
    public void test7(){
        List<Man2> integers = Arrays.asList(
                new Man2(1,"2","男",1.0),
                new Man2(2,"2","男",2.0),
                new Man2(3,"3","女",3.0)
        );
        //普通拼接
        String collect = integers.stream().map(Man2::getName).collect(Collectors.joining());
        //指定分割符
        String collect2 = integers.stream().map(Man2::getName).collect(Collectors.joining(",","{","}"));
        // 拼接时拼接首尾的符号
        System.out.println(collect2);
    }


    /**
     * 串行流  并行流
     * 并行流
     */
    //并行流   parallel
    @Test
    public void test8(){
        long start = System.currentTimeMillis();

        long reduce = LongStream.rangeClosed(8, 146464646L).parallel().reduce(0, Long::sum);
        System.out.println(reduce);


        long end = System.currentTimeMillis();
        System.out.println("耗时"+(end-start));
    }
    //串行流  sequential
    @Test
    public void test9(){
        long start = System.currentTimeMillis();

        long reduce = LongStream.rangeClosed(8, 146464646L).sequential().reduce(0, Long::sum);
        System.out.println(reduce);


        long end = System.currentTimeMillis();
        System.out.println("耗时"+(end-start));
    }

 

上一篇:JDK1.8 Stream Collectors常用方法


下一篇:三 Prometheus 监控 Mysql