zoukankan      html  css  js  c++  java
  • 高效 告别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
  • 相关阅读:
    小程序行内点击事件冲突解决
    小程序带参返回刷新主页面
    小程序时间选择器(精确到秒)
    Bootstrap-Table事件和方法
    JS 解决txt文件直接打开而不是下载
    Bootstrap-Table进阶篇
    Bootstrap-Table入门篇
    Angular+SSM+Ajax的简单购物车实例
    Angular实现简单购物车
    K3BOS单据获取单据体行数
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/12873911.html
Copyright © 2011-2022 走看看