对象准备:
public class Student {
//学生年级
private String grade;
//参加的科目
private String subject;
//名字
private String name;
//科目成绩
private Double mark;
public Student(String grade, String subject, String name, Double mark) {
this.grade = grade;
this.subject = subject;
this.name = name;
this.mark = mark;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMark() {
return mark;
}
public void setMark(Double rank) {
this.mark = rank;
}
@Override
public String toString() {
return "Student{" +
"grade='" + grade + '\'' +
", subject='" + subject + '\'' +
", name='" + name + '\'' +
", mark='" + mark + '\'' +
'}';
}
}
构建数据
List students = new ArrayList<>();
Student student1 = new Student("六年级","Math","小张",86D);
Student student2 = new Student("六年级","English","小张",84D);
Student student3 = new Student("六年级","Chinese","小张",83D);
Student student1s = new Student("六年级","Math","小宋",84D);
Student student2s = new Student("六年级","English","小宋",95D);
Student student3s = new Student("六年级","Chinese","小宋",83D);
students.add(student1);
students.add(student1s);
students.add(student2);
students.add(student2s);
students.add(student3);
students.add(student3s);
Student student4 = new Student("五年级","Math","小杨",78D);
Student student5 = new Student("五年级","English","小杨",97D);
Student student6 = new Student("五年级","Chinese","小杨",68D);
Student student4s = new Student("五年级","Math","小赵",67D);
Student student5s = new Student("五年级","English","小赵",93D);
Student student6s = new Student("五年级","Chinese","小赵",92D);
students.add(student4);
students.add(student4s);
students.add(student5);
students.add(student5s);
students.add(student6);
students.add(student6s);
数据分组操作
1.单条件分组
根据姓名进行分组:
/*按名字分组*/
Map> map = students.stream().collect(Collectors.groupingBy(Student::getName));
System.out.println(map);
Set ketSet = map.keySet();
System.out.println(ketSet);
for (Object key:ketSet){
System.out.println(key + " 同学:");
System.out.println(map.get(key));
}
2.多条件分组
先按照分数进行分组,再按照科目进行分组
/*先根据成绩分组,再根据名字分组*/
Map>> map2 =
students.stream().collect(Collectors.groupingBy(Student::getMark,Collectors.groupingBy(Student::getSubject)));
System.out.println(map2);
Set ketSet2 = map2.keySet();
System.out.println(ketSet2);
for (Object key:ketSet2){
System.out.println(map2.get(key));
System.out.println("==============");
}
3.单条件分组,按照自定义条件分组
//找出所有成绩在85之上的学生(包括85)分为合格,85之下的为不合格
public static void getMarkByStandard(){
Map> map = students.stream().collect(Collectors.groupingBy(student ->{
if (student.getMark().intValue() >= 85){
return "合格";
}else{
return "不合格";
}
}));
System.out.println(map.get("合格"));
System.out.println(map.get("不合格"));
}
4.多条件分组,按照自定义条件分组
上面两个例子的分组产生的Map的key值都是直接取的对象里的值,其实也可以自己规定分组的规则和key的名字,我们可以在Lambda的右边使用大括号,在大括号里面写逻辑代码,只要保证返回的值是符合格式的就可以,比如现在就是以85分为界限进行分组,Map的key值也也就是我们返回的两个值。
Map>> map = students.stream().collect(Collectors.groupingBy(Student::getGrade,
Collectors.groupingBy(student -> {
if (student.getMark().intValue() >= 85){
return "合格";
}else{
return "不合格";
}
})));
System.out.println(map);
Set ketSet = map.keySet();
System.out.println(ketSet);
System.out.println(map.get("五年级"));
System.out.println(map.get("六年级"));
5.分组数量统计
5.1.单条件分组数量统计
students.stream().collect(Collectors.groupingBy(EnergyLog::getMark,Collectors.counting()));
5.2.多条件分组数量统计
有时候我们只是想要统计每个分组的数量,而不是获得每个分组里面元素的详细信息,这个也是可以实现的。只是在上述的二级分组基础上调用了Collectors.counting()
方法来统计每个分组里面的数量
Map> map = students.stream().collect(Collectors.groupingBy(Student::getGrade,
Collectors.groupingBy(student -> {
if (student.getMark().intValue() >= 85){
return "合格";
}else{
return "不合格";
}
},Collectors.counting())
)
);
System.out.println(map);
输出:{五年级={合格=3, 不合格=3}, 六年级={不合格=4, 合格=2}}
6. 取最大值
在分组的基础上获得分数最高的学生的信息
Map> map = students.stream().collect(Collectors.groupingBy(Student::getGrade,
Collectors.maxBy(Comparator.comparing(Student::getMark))));
System.out.println(map);
输出:{五年级=Optional[Student{grade='五年级', age=11, subject='English', name='小杨', mark='97.0'}], 六年级=Optional[Student{grade='六年级', age=12, subject='English', name='小宋', mark='95.0'}]}
7.分区
分区其实就是特殊的分组,它只是固定将流中的数据分为两组,大致用法其实和分组一样。
public static void partitionByStandard(){
Map> map = students.stream().collect(Collectors.groupingBy(Student::getGrade,
Collectors.partitioningBy(student -> {
if (student.getMark().intValue() >= 85){
return true;
}else{
return false;
}
},Collectors.counting())
)
);
System.out.println(map);
}
输出:{六年级={false=4, true=2}, 五年级={false=3, true=3}}