lambda处理分组汇总等功能

该篇博客展示了Java中List的创建与操作,通过stream进行数据分组、过滤、求和与排序等操作。涉及内容包括:学生对象列表、分组聚合、过滤特定条件、排序等核心Java集合与流处理技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 List<Student> studentList = new ArrayList();
        studentList.add(new Student("a", "am1", 1L, 5, new BigDecimal(3)));
        studentList.add(new Student("a", "am1", 2L, 4, new BigDecimal(3)));
        studentList.add(new Student("c", "am1", 3L, 3, new BigDecimal(5)));
        studentList.add(new Student("b", "bm2", 4L, 3, new BigDecimal(3)));
        studentList.add(new Student("b", "bm3", 4L, 6, new BigDecimal(3)));

        studentList.forEach(student -> {
            System.out.println(student.toString());
        });
        System.out.println("=========================原始数据=========================");
        Map<String, List<Student>> map = studentList.stream().collect(
                Collectors.groupingBy(Student::getCode));
        // code 分组后  求和
        for (String key : map.keySet()) {
            System.out.println(key);
            List<Student> list = map.get(key);
            Optional<Student> student=list.stream().collect(Collectors.reducing((sum, s) -> new Student(s.code, s.name, sum.chengJi+s.chengJi, sum.age + s.age, sum.value.add(s.value))));
            list.forEach(obj->{
                System.out.println(obj.toString());
            });
            System.out.println(student.toString());
        }
        System.out.println(map.size());
        System.out.println("========================================================");
        
        // 组合分组 然后 List
        Map<String,List<Student>> mstu=studentList.stream().collect(Collectors.groupingBy(student -> {return student.code+"#"+student.name;}));
        mstu.forEach((key,values)->{
            System.out.println("KEY:"+key);
            System.out.println("VALUE:"+values.size());
        });

        System.out.println("========================filter 过滤================================");
        List<Student> studentListOne=studentList.stream().filter(student->{return student.name=="am1";} ).collect(Collectors.toList());
        studentListOne.forEach(student -> {
            System.out.println(student.toString());
        });

        System.out.println("========================filter 过滤---> 分组================================");
        Map<String,List<Student>> mapListFilter=studentList.stream().filter(student->{return student.name=="am1";} ).collect(Collectors.toList()).stream().collect(Collectors.groupingBy(Student::getCode,Collectors.toList()));
        for (Map.Entry<String, List<Student>> mapStudent:mapListFilter.entrySet()) {
            System.out.println("mapStudent:"+mapStudent.getKey());
            System.out.println("====value========");
            mapStudent.getValue().forEach(System.out::println);
        }
        System.out.println("========================sort 排个序================================");
        studentList.forEach(student -> {
            System.out.println(student.toString());
        });
        System.out.println("=排序后===");
        List<Student> stuListSorted = studentList.stream().sorted((stu1,stu2)-> { return stu2.age-stu1.age;}).collect(Collectors.toList());
        stuListSorted.forEach(student -> {
            System.out.println(student.toString());
        }); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值