Java8 新特性 Stream流操作

数据准备

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(学生班级)进行分组,对语文,数学,英语的分数进行分组统计
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你们卷的我睡不着QAQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值