java多字段分组

该文章展示了如何在Java中利用StreamAPI的Collectors工具类对对象集合进行单字段和多字段(组合学校和性别字段)的分组操作,以便按特定标准聚合数据。代码示例中,创建了一个Student类和对应的测试数据,然后通过groupingBy方法实现了分组功能,并打印了分组结果。
摘要由CSDN通过智能技术生成

java多字段分组

完整代码见最后!!!

单字段分组

		//根据学校字段分组
        Map<String, List<Student>> stuMap = stus.stream().collect(Collectors.groupingBy(stu -> stu.getSchool()));
        for (Map.Entry<String, List<Student>> stringListEntry : stuMap.entrySet()) {
            System.out.println("分组键:" + stringListEntry.getKey());

            //获取学校
            String school = stringListEntry.getKey();
            System.out.println("学校:" + school);

            //获取学生
            List<Student> students = stringListEntry.getValue();
            System.out.println("学生:" + students);
        }

输出:

分组键:A学校
学校:A学校
学生:[Student{name='赵', school='A学校', score=69, sex='男'}, Student{name='钱', school='A学校', score=79, sex='女'}, Student{name='孙', school='A学校', score=89, sex='女'}, Student{name='李', school='A学校', score=99, sex='男'}]
分组键:B学校
学校:B学校
学生:[Student{name='周', school='B学校', score=69, sex='男'}, Student{name='吴', school='B学校', score=79, sex='女'}, Student{name='郑', school='B学校', score=89, sex='女'}, Student{name='王', school='B学校', score=99, sex='男'}]

多字段分组

		//根据学校、性别两个字段分组
        stuMap = stus.stream().collect(Collectors.groupingBy(stu -> stu.getSchool() + "#" + stu.getSex()));
        for (Map.Entry<String, List<Student>> stringListEntry : stuMap.entrySet()) {
            //获取分组键
            String[] key = stringListEntry.getKey().split("#");
            System.out.println("分组键:" + Arrays.toString(key));

            //获取学校
            String school = key[0];
            System.out.println("学校:" + school);

            //获取性别
            String sex = key[1];
            System.out.println("性别:" + sex);

            //获取学生
            List<Student> students = stringListEntry.getValue();
            System.out.println("学生:" + students);
        }

输出:

分组键:[B学校, 男]
学校:B学校
性别:男
学生:[Student{name='周', school='B学校', score=69, sex='男'}, Student{name='王', school='B学校', score=99, sex='男'}]
分组键:[A学校, 男]
学校:A学校
性别:男
学生:[Student{name='赵', school='A学校', score=69, sex='男'}, Student{name='李', school='A学校', score=99, sex='男'}]
分组键:[B学校, 女]
学校:B学校
性别:女
学生:[Student{name='吴', school='B学校', score=79, sex='女'}, Student{name='郑', school='B学校', score=89, sex='女'}]
分组键:[A学校, 女]
学校:A学校
性别:女
学生:[Student{name='钱', school='A学校', score=79, sex='女'}, Student{name='孙', school='A学校', score=89, sex='女'}]

完整代码:

package com.study.demo_study;

public class Student {
    private String name;
    private String school;
    private int score;
    private String sex;

    public Student(String name, String school, int score, String sex) {
        this.name = name;
        this.school = school;
        this.score = score;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", school='" + school + '\'' +
                ", score=" + score +
                ", sex='" + sex + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

package com.study.demo_study;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GroupTest {
    public static void main(String[] args) {
        List<Student> stus = new ArrayList<>();
        stus.add(new Student("赵", "A学校", 69, "男"));
        stus.add(new Student("钱", "A学校", 79, "女"));
        stus.add(new Student("孙", "A学校", 89, "女"));
        stus.add(new Student("李", "A学校", 99, "男"));

        stus.add(new Student("周", "B学校", 69, "男"));
        stus.add(new Student("吴", "B学校", 79, "女"));
        stus.add(new Student("郑", "B学校", 89, "女"));
        stus.add(new Student("王", "B学校", 99, "男"));

        //根据学校字段分组
        Map<String, List<Student>> stuMap = stus.stream().collect(Collectors.groupingBy(stu -> stu.getSchool()));
        for (Map.Entry<String, List<Student>> stringListEntry : stuMap.entrySet()) {
            System.out.println("分组键:" + stringListEntry.getKey());

            //获取学校
            String school = stringListEntry.getKey();
            System.out.println("学校:" + school);

            //获取学生
            List<Student> students = stringListEntry.getValue();
            System.out.println("学生:" + students);
        }

        //根据学校、性别两个字段分组
        stuMap = stus.stream().collect(Collectors.groupingBy(stu -> stu.getSchool() + "#" + stu.getSex()));
        for (Map.Entry<String, List<Student>> stringListEntry : stuMap.entrySet()) {
            //获取分组键
            String[] key = stringListEntry.getKey().split("#");
            System.out.println("分组键:" + Arrays.toString(key));

            //获取学校
            String school = key[0];
            System.out.println("学校:" + school);

            //获取性别
            String sex = key[1];
            System.out.println("性别:" + sex);

            //获取学生
            List<Student> students = stringListEntry.getValue();
            System.out.println("学生:" + students);
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值