数据准备
package test;
/**
* [一句话描述该类的功能]
*
* @author : [61692]
* @version : [v1.0]
* @createTime : [2024/3/31 14:52]
*/
public class Student {
private int id;
private int age;
private int yuwenScore;
private int mathScore;
private String name;
private int yingyuScore;
private String stuClass;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuClass() {
return stuClass;
}
public void setStuClass(String stuClass) {
this.stuClass = stuClass;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getYuwenScore() {
return yuwenScore;
}
public void setYuwenScore(int yuwenScore) {
this.yuwenScore = yuwenScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYingyuScore() {
return yingyuScore;
}
public void setYingyuScore(int yingyuScore) {
this.yingyuScore = yingyuScore;
}
// public Student(int age, int yuwenScore, int mathScore, String name, int yingyuScore) {
// this.age = age;
// this.yuwenScore = yuwenScore;
// this.mathScore = mathScore;
// this.name = name;
// this.yingyuScore = yingyuScore;
// }
public Student() {
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", yuwenScore=" + yuwenScore +
", mathScore=" + mathScore +
", name='" + name + '\'' +
", yingyuScore=" + yingyuScore +
'}';
}
}
Student student = new Student();
student.setAge(12);
student.setName("zhang");
student.setId(1);
student.setStuClass("1班");
student.setMathScore(85);
student.setYingyuScore(80);
student.setYuwenScore(75);
Student student1 = new Student();
student1.setAge(14);
student1.setId(2);
student1.setName("li");
student1.setStuClass("2班");
student1.setMathScore(67);
student1.setYingyuScore(84);
student1.setYuwenScore(71);
Student student2 = new Student();
student2.setAge(14);
student2.setId(3);
student2.setName("li");
student2.setStuClass("2班");
student2.setMathScore(89);
student2.setYingyuScore(83);
student2.setYuwenScore(88);
Student student3 = new Student();
student3.setAge(14);
student3.setId(4);
student3.setName("li");
student3.setStuClass("1班");
student3.setMathScore(76);
student3.setYingyuScore(91);
student3.setYuwenScore(92);
List<Student> students = new ArrayList<>();
students.add(student);
students.add(student1);
students.add(student2);
students.add(student3);
(1)过滤操作
List<Student> studentList = students.stream().filter(item -> item.getAge() == 12).collect(Collectors.toList());
for(Student item : studentList){
System.out.println("item.toString() = " + item.toString());
}
使用filter操作时,一定要注意,filter不是一个void类型的方法,因此下面的操作时错误的:
students.stream().filter(item -> item.getAge() == 12).collect(Collectors.toList());
students数组不会过滤age =12
不信,我们可以验证一下。
students并没有实现过滤,
因此,需要 List studentList = students.stream().filter(item -> item.getAge() == 12).collect(Collectors.toList());
(2)抽取属性值
我们开发时是不是经常会将每个数组中的id抽取成一个idlist.
那么Stream中的map()可以帮助到你
List<Integer> list1 = students.stream().map(Student::getAge).toList();
for(Integer item : list1){
System.out.println("item.toString() = " + item.toString());
}
抽取StudentId
(3)数据分组 GroupBy
Map<String,Map<String,Integer>> result1 = students.stream().collect(
Collectors.groupingBy(Student::getStuClass,Collectors.mapping(
studentItem -> {
Map<String,Integer> groupByResult= new HashMap<>();
groupByResult.put("yuwenScore",studentItem.getYuwenScore());
groupByResult.put("mathScore",studentItem.getMathScore());
groupByResult.put("yingyuScore",studentItem.getYingyuScore());
return groupByResult;
},Collectors.reducing(new HashMap<>(), (map1,map2) ->{
Map<String,Integer> resultMapping = new HashMap<>(map1);
map2.forEach((key,value) -> resultMapping.merge(key, value, Integer::sum));
return resultMapping;
})
))
);
// 输出每个班级的成绩
result1.forEach((stuClass, scores) -> {
System.out.println(stuClass + ":");
scores.forEach((subject, totalScore) -> System.out.println(subject + ": " + totalScore));
});
我们对StuClass(学生班级)进行分组,对语文,数学,英语的分数进行分组统计