Java8 Stream Step3 终止操作(二)

package com.xbb.java.stream;


import java.util.*;
import java.util.stream.Collectors;

/**
 * 3 : 终止操作
 *
 *      归约 : 将流中元素反复结合起来得到一个值
 *      收集 : 将流转换为其他形式.接收一个Collector接口的实现.用于给Stream中元素做汇总的方法
 *
 */
public class StreamDemo_Step_3_2 {


    public static void main(String[] args) {

        step3_reduce();
        step3_collect();
    }

    /**
     * 归约
     */
    public static void step3_reduce(){
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8);
        // 普通遍历求和
        System.out.println(list.stream().mapToInt((x)->x).sum());
        // reduce求和
        // 原理 : 以第一个参数为起始值开始.把第个参数当作为0,从流中取第一个元素当作y,让x与y相加.
        //       之后把xy的和当作这二次运算的x,再从流中取出下一个元素进行相加.以此类推.
        System.out.println(list.stream().reduce(0,(x,y)->x+y));
        System.out.println(list.stream().reduce(Integer::sum).get());

    }

    /**
     * 收集,把人员集合中的名字都收集出来放在集合中.
     */
    public static void step3_collect(){
        List<Person> persons = Arrays.asList(
                new Person(1,"小败笔-1",32,"china"),
                new Person(2,"小败笔-2",52,"america"),
                new Person(3,"小败笔-3",72,"england"),
                new Person(4,"小败笔-4",12,"america"),
                new Person(5,"小败笔-5",52,"china"),
                new Person(6,"小败笔-5",52,"america")
        );
        // 把名字收集到List
        persons.stream()
                // 提取名字
                .map(Person::getName)
                // 把名字收集到list中去
                .collect(Collectors.toList())
                .forEach(System.out::println);
        System.out.println("------------------");
        // 把名字收集到Set
        persons.stream()
                // 提取名字
                .map(Person::getName)
                // 把名字收集到list中去
                .collect(Collectors.toSet())
                .forEach(System.out::println);
        System.out.println("------------------");
        // 把名字收集到HashSet中
        persons.stream()
                .map(Person::getName)
                .collect(Collectors.toCollection(HashSet::new))
                .forEach(System.out::println);
        System.out.println("------------------");

        //获取总数
        Long count = persons.stream()
                .map(Person::getName)
                .count();
        System.out.println(count);
        count = persons.stream()
                .collect(Collectors.counting());
        System.out.println(count);

        // 获取平均值
        Double avgAge = persons.stream()
                .collect(Collectors.averagingInt(Person::getAge));
        System.out.println(avgAge);

        // 求年龄的总和
        Double sumAge = persons.stream()
                .collect(Collectors.summingDouble(Person::getAge));
        System.out.println(sumAge);

        // 求最大值
        int maxValue = persons.stream().map(Person::getAge).max(Integer::compareTo).get();
        System.out.println(maxValue);

        maxValue = persons.stream()
                .map(Person::getAge)
                .collect(Collectors.maxBy(Integer::compareTo)).get();

        Optional<Person> person = persons
                .stream()
                .collect(Collectors.maxBy((p1,p2)->Integer.compare(p1.getAge(),p2.getAge())));
        System.out.println(person.get().getName()+"-"+person.get().getAge());

        // 分组求平均值
        Map<String,List<Person>> maps = persons.stream()
                .collect(Collectors.groupingBy(Person::getCouny));
        System.out.println(maps.get("china"));

        // 多级分组
        Map<String,Map<String,List<Person>>> maps2 = persons.stream()
                .collect(Collectors.groupingBy(Person::getCouny,Collectors.groupingBy((person1)->{
                    if (person1.getAge()>30) return "小同志";
                    else return "老年人";
                })));
        System.out.println(maps2.get("america").get("小同志"));

        // 分片或分区
        // 按年龄分区
        Map<Boolean,List<Person>> maps3 = persons.stream()
                .collect(Collectors.partitioningBy((p)->p.getAge()>35));
        System.out.println(maps3);

        // 计算各种数据值
        DoubleSummaryStatistics dss = persons.stream().collect(Collectors.summarizingDouble(Person::getAge));
        System.out.println(dss.getMax());
        System.out.println(dss.getAverage());
        System.out.println(dss.getSum());
        System.out.println(dss.getCount());
        System.out.println(dss.getMin());

        // 把集合中的名字连接到一块
        String names = persons.stream().map(Person::getName).collect(Collectors.joining(","));
        System.out.println(names);
     }
}

猜你喜欢

转载自blog.csdn.net/jinjianghai/article/details/92967363