Java关于stream处理数据的一些操作

发布时间 2023-11-01 11:04:20作者: 十一点
class UserPo {
    private String name;
    private Double score;
}
 
List<UserPo> list = new ArrayList<>();
        list.add(new UserPo("小一", 10.d));
        list.add(new UserPo("小五", 50.d));
        list.add(new UserPo("小六", 60.d));
        list.add(new UserPo("小6", 60.d));
        list.add(new UserPo("小空", null));
        list.add(new UserPo("小九", 90.d));
 

        long count = 0;
        List<UserPo> filterList = null;

        // filter 过滤器的使用
        // 筛选出成绩不为空的学生人数
        count = list.stream().filter(p -> null != p.getScore()).count();
        System.out.println("参加考试的学生人数:" + count);

        // collect
        // 筛选出成绩不为空的学生集合
        filterList = list.stream().filter(p -> null != p.getScore()).collect(Collectors.toList());
        System.out.println("参加考试的学生信息:");
        filterList.stream().forEach(System.out::println);

        // map 将集合映射为另外一个集合
        // 取出所有学生的成绩
        List<Double> scoreList = list.stream().map(p -> p.getScore()).collect(Collectors.toList());
        System.out.println("所有学生的成绩集合:" + scoreList);

        // map
        System.out.println("按学生成绩归集:");
        Map<Double, List<UserPo>> groupByScoreMap = list.stream().filter(p -> null != p.getScore()).collect(Collectors.groupingBy(UserPo::getScore));
        for (Map.Entry<Double, List<UserPo>> entry : groupByScoreMap.entrySet()) {
            System.out.println("成绩:" + entry.getKey() + " 人数:" + entry.getValue().size());
        }


        // 将学生姓名集合串成字符串,用逗号分隔
        String nameString = list.stream().map(p -> p.getName()).collect(Collectors.joining(","));
        System.out.println("所有学生的姓名字符串:" + nameString);

        // sorted排序
        // 按学生成绩逆序排序 正序则不需要加.reversed()
        filterList = list.stream().filter(p -> null != p.getScore()).sorted(Comparator.comparing(UserPo::getScore).reversed()).collect(Collectors.toList());
        System.out.println("所有学生的成绩集合,逆序排序:");
        filterList.stream().forEach(System.out::println);

        // forEach
        filterList.stream().forEach(p -> p.setScore(p.getScore() + 10));
        System.out.println("及格人数太少,给每个人加10分");
        filterList.stream().forEach(System.out::println);

        // count
        count = filterList.stream().filter(p -> p.getScore() >= 60).count();
        System.out.println("最后及格人数" + count);

        DoubleSummaryStatistics statistics = filterList.stream().mapToDouble(p -> p.getScore()).summaryStatistics();
        System.out.println("列表中最大的数 : " + statistics.getMax());
        System.out.println("列表中最小的数 : " + statistics.getMin());
        System.out.println("所有数之和 : " + statistics.getSum());
        System.out.println("平均数 : " + statistics.getAverage());

        // 并行流 使用
        count = list.parallelStream().filter(p -> null != p.getScore()).count();
        System.out.println("并行流处理参加考试的学生人数:" + count);
 
        // BigDecimal 求和
        // BigDecimal baseQtySum = stkInventories.stream().map(StkInventory::getBaseQty).reduce(BigDecimal.ZERO, BigDecimal::add);
 
List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 10, 23, "male", "New York", null));
        personList.add(new Person("Tom", 11, 23, "male", "Washington1", null));
        personList.add(new Person("Tom", 11, 21, "female", "Washington", null));
        personList.add(new Person("Anni", 110, 24, "female", "New York", null));
        personList.add(new Person("Alisa", 111, 26, "male", "New York", null));
        personList.add(new Person("Alisa", 111, 25, "female", "New York", null));
 
        //根据一个属性合并
        // List<Person> result = new ArrayList<>(personList.stream()
        //         .collect(Collectors.toMap(Person::getName, a -> a, (o1, o2) -> {
        //             o1.setSalary(o1.getSalary() + o2.getSalary());
        //             return o1;
        //         })).values());
        // result.forEach(System.out::println);
 
  //根据两个属性合并
        // List<Person> list = new ArrayList<>();
        // personList.parallelStream().collect(Collectors.groupingBy(o->(o.getName() + o.getAge() + o.getArea()),Collectors.toList()))
        //         .forEach((id,transfer) -> transfer.stream().reduce((a, b) ->
        //                         new Person(a.getName(), a.getAge(),a.getSalary() + b.getSalary(), a.getSex(), a.getArea(), null))
        //                 .ifPresent(list::add));
        //
        // list.forEach(System.out::println);
 
        // 设置第三属性分组再合并
        personList.forEach(a -> a.setNameAge(a.getName() + a.getAge()));
        List<Person> result1 = new ArrayList<>(personList.stream()
                .collect(Collectors.toMap(Person::getNameAge, a -> a, (o1, o2) -> {
                    o1.setSalary(o1.getSalary() + o2.getSalary());
                    return o1;
                })).values());
        result1.forEach(System.out::println);