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);
}
}
}