Stream anyMatch查找案例
生活随笔
收集整理的這篇文章主要介紹了
Stream anyMatch查找案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.gblfy.gxts;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;/*** 案例1:* 某班集中有20名學生,每名學生有5門課的考試成績。* 其中缺考的科目分數字段為空,需要找出缺考的學生都是誰?*/
public class CaseTwo {/*** 考試成績模型*/@Data@AllArgsConstructorstatic class ExamStudentSore {/*** 考生姓名*/private String studentName;/*** 考試成績*/private Integer scoreValue;/*** 科目*/private String subject;}//初始化考試成績容器Map<String, List<ExamStudentSore>> studentMap;//初始化數據@Beforepublic void init() {//聲明考試成績容器studentMap = new HashMap<>();//張三考試成績List<ExamStudentSore> zsScoreList = new ArrayList<>();zsScoreList.add(new ExamStudentSore("張三", 80, "CHINESE"));zsScoreList.add(new ExamStudentSore("張三", 90, "MATHS"));zsScoreList.add(new ExamStudentSore("張三", 100, "ENGLISH"));zsScoreList.add(new ExamStudentSore("張三", 100, "BIOLOGICAL"));zsScoreList.add(new ExamStudentSore("張三", 80, "SPORTS"));//王五考試成績List<ExamStudentSore> wwScoreList = new ArrayList<>();wwScoreList.add(new ExamStudentSore("王五", 80, "CHINESE"));wwScoreList.add(new ExamStudentSore("王五", null, "MATHS"));wwScoreList.add(new ExamStudentSore("王五", 100, "ENGLISH"));wwScoreList.add(new ExamStudentSore("王五", 100, "BIOLOGICAL"));wwScoreList.add(new ExamStudentSore("王五", 80, "SPORTS"));//趙六考試成績List<ExamStudentSore> zlScoreList = new ArrayList<>();zlScoreList.add(new ExamStudentSore("趙六", null, "CHINESE"));zlScoreList.add(new ExamStudentSore("趙六", 90, "MATHS"));zlScoreList.add(new ExamStudentSore("趙六", 100, "ENGLISH"));zlScoreList.add(new ExamStudentSore("趙六", 100, "BIOLOGICAL"));zlScoreList.add(new ExamStudentSore("趙六", 80, "SPORTS"));//將張三、王五趙六成績放入考試成績容器studentMap.put("張三", zsScoreList);studentMap.put("王五", wwScoreList);studentMap.put("趙六", zlScoreList);}/*** 查找考試缺考的學生名單列表*/@Testpublic void findNoScoreStudentList() {studentMap.forEach((studentName, studentScoreList) -> {boolean bool = studentScoreList.stream().anyMatch(score -> {return score.getScoreValue() == null;});if (bool) {System.out.println("此學生【" + studentName + "】存在缺考的現象!");}});}
}
總結
以上是生活随笔為你收集整理的Stream anyMatch查找案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Stream filter过滤案例
- 下一篇: 安装rzsz