高效 告别996,开启java高效编程之门 3-25实战案例一:查找

1    重点:

1.1  设计思路:用anyMatch 不是 findAny

2    查找demo

查找demo需求:

多个学生有多门课程,查找出缺考的学生姓名

测试类:

package com.imooc.zhangxiaoxi.stream.cases;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CaseOne
 *  需求:20名学生,没名学生5门课,缺考科目字段为空,求缺考学生的姓名
 * @author 魏豆豆
 * @date 2020/5/10
 */
public class CaseOne {

    /**
     * 考试成绩模型
     */
    /*@Data
    @AllArgsConstructor*/
    class ExamStudentScore{
        private String studentName;     //学生姓名
        private Integer scoreValue;      //成绩
        private String subject;         //科目

        public ExamStudentScore(String studentName, Integer scoreValue, String subject) {
            this.studentName = studentName;
            this.scoreValue = scoreValue;
            this.subject = subject;
        }

        public String getStudentName() {
            return studentName;
        }

        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }

        public Integer getScoreValue() {
            return scoreValue;
        }

        public void setScoreValue(Integer scoreValue) {
            this.scoreValue = scoreValue;
        }

        public String getSubject() {
            return subject;
        }

        public void setSubject(String subject) {
            this.subject = subject;
        }
    }

    /**
     * 学生考试成绩
     */
    Map<String, List<ExamStudentScore>> studentMap;

    @Before
    public void init() {
        studentMap = new HashMap<>();

        List<ExamStudentScore> zsScoreList = new ArrayList<>();
        zsScoreList.add(
                new ExamStudentScore(
                        "张三",
                        30,
                        "CHINESE"));
        zsScoreList.add(
                new ExamStudentScore(
                        "张三",
                        40,
                        "ENGLISH"));
        zsScoreList.add(
                new ExamStudentScore(
                        "张三",
                        50,
                        "MATHS"));
        studentMap.put("张三", zsScoreList);

        List<ExamStudentScore> lsScoreList = new ArrayList<>();
        lsScoreList.add(new ExamStudentScore("李四",80,"CHINESE"));
        lsScoreList.add(
                new ExamStudentScore(
                        "李四",
                        null,
                        "ENGLISH"));
        lsScoreList.add(
                new ExamStudentScore(
                        "李四",
                        100,
                        "MATHS"));
        studentMap.put("李四", lsScoreList);

        List<ExamStudentScore> wwScoreList = new ArrayList<>();
        wwScoreList.add(
                new ExamStudentScore(
                        "王五",
                        null,
                        "CHINESE"));
        wwScoreList.add(
                new ExamStudentScore(
                        "王五",
                        null,
                        "ENGLISH"));
        wwScoreList.add(
                new ExamStudentScore(
                        "王五",
                        70,
                        "MATHS"));
        studentMap.put("王五", wwScoreList);
    }

    @Test
    public void findStudent(){
        studentMap.forEach((studentname,scoreList)->{
            boolean bool = scoreList.stream().anyMatch(score->{
               return score.getScoreValue()==null;
            });
            if (bool){
                System.out.println("缺考学生姓名为:"+studentname);
            }
        });

    }
}

打印日志:

缺考学生姓名为:李四
缺考学生姓名为:王五

Process finished with exit code 0
原文地址:https://www.cnblogs.com/1446358788-qq/p/12873911.html