Java—stream以及集合框架使用
生活随笔
收集整理的這篇文章主要介紹了
Java—stream以及集合框架使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1) 編寫(xiě)Student類(lèi),主要屬性包括學(xué)號(hào)、姓名、性別、班級(jí)
2) 編寫(xiě)Score類(lèi),主要屬性包括:學(xué)號(hào)、課程名、分?jǐn)?shù)
3) 模擬期末考試的成績(jī)統(tǒng)計(jì)應(yīng)用場(chǎng)景,要求
(1) 所有學(xué)生名單及對(duì)應(yīng)科目成績(jī)已經(jīng)初始化在數(shù)組中
(2) 要求輸出每門(mén)課程的所有學(xué)生的平均成績(jī)。
(3) 輸出某門(mén)課程的最高成績(jī),最低成績(jī)
(4) 可以查詢(xún)某個(gè)學(xué)生的所有成績(jī)。
源代碼
二、源程序 Score類(lèi) public class Score {private String studentID;private String course;private int grades;public Score(String studentID, String course, int grades) {this.studentID = studentID;this.course = course;this.grades = grades;}public String getStudentID() {return studentID;}public void setStudentID(String studentID) {this.studentID = studentID;}public String getCourse() {return course;}public void setCourse(String course) {this.course = course;}public int getGrades() {return grades;}public void setGrades(int grades) {this.grades = grades;} }Student類(lèi) public class Student {private String studentID;private String studentName;private String sex;private String classID;public Student(String studentID, String studentName, String sex, String classID) {this.studentID = studentID;this.studentName = studentName;this.sex = sex;this.classID = classID;}public String getStudentID() {return studentID;}public void setStudentID(String studentID) {this.studentID = studentID;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getClassID() {return classID;}public void setClassID(String classID) {this.classID = classID;} }Util類(lèi) public class Util {public static void getAverage(Score[] scores) {Set<String> set = new HashSet<>();for (Score score : scores)set.add(score.getCourse());for (String course : set)System.out.println(course + "平均成績(jī):" + Arrays.stream(scores).filter(x -> x.getCourse().equals(course)).mapToInt(Score::getGrades).summaryStatistics().getAverage());}public static void getMax(Score[] scores, String course) {IntSummaryStatistics statistics = Arrays.stream(scores).filter(x -> x.getCourse().equals(course)).mapToInt(Score::getGrades).summaryStatistics();System.out.println(course + "最高成績(jī):" + statistics.getMax());System.out.println(course + "最低成績(jī):" + statistics.getMin());}public static void getAllGrade(Score[] scores, Student[] students, String studentName) {String studentID = null;for (Student student : students)if (student.getStudentName().equals(studentName))studentID = student.getStudentID();for (Score score : scores)if (studentID.equals(score.getStudentID()))System.out.println(studentName + " " + score.getCourse() + "成績(jī)?yōu)?#xff1a;" + score.getGrades());}public static void main(String[] args) {Score[] scores = new Score[]{new Score("1", "math", 89), new Score("1", "english", 90), new Score("2", "math", 92), new Score("2", "english", 95)};Student[] student = new Student[]{new Student("1", "mary", "women", "1"), new Student("2", "john", "men", "1")};getAverage(scores);getMax(scores, "math");getAllGrade(scores, student, "john");} }總結(jié)
以上是生活随笔為你收集整理的Java—stream以及集合框架使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到自己复合了是什么预兆
- 下一篇: Java 8 的List<V> 转成 M