Java—stream以及集合框架使用

1) 編寫Student類,主要屬性包括學號、姓名、性別、班級
2) 編寫Score類,主要屬性包括:學號、課程名、分數
3) 模擬期末考試的成績統計應用場景,要求
(1) 所有學生名單及對應科目成績已經初始化在數組中
(2) 要求輸出每門課程的所有學生的平均成績。
(3) 輸出某門課程的最高成績,最低成績
(4) 可以查詢某個學生的所有成績。

源代碼

二、源程序
Score類
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類
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類
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 + "平均成績:" + 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 + "最高成績:" + statistics.getMax());System.out.println(course + "最低成績:" + 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() + "成績為:" + 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");}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392013.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392013.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392013.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

山東省2021年高考成績查詢平臺6,山東2021年高考成績改為6月26日前公布

6月11日&#xff0c;山東省教育廳舉行2021年第一次高考新聞發布會&#xff0c;介紹2021年高考基本情況、評卷安排、成績公布等相關工作。山東省教育招生考試院新聞發言人、普招處處長李春光介紹&#xff0c;根據近期國家有關工作要求和強基計劃招生工作需要&#xff0c;原定于6…

如何在vuejs里禁用eslint語法檢查工具

eslint好是好&#xff0c;可要求很苛刻&#xff0c;對于我這種寫代碼很糙的媛。。。。。。 搜索的時候有的說加入 /* eslint-disabled */&#xff08;有用&#xff0c;但只是部分代碼享受此待遇&#xff09; 還有說刪除.eslintrc.js里包含eslint關鍵字的塊&#xff0c;a---o---…

數據結構兩個月學完_這是我作為數據科學家兩年來所學到的

數據結構兩個月學完It has been 2 years ever since I started my data science journey. Boy, that was one heck of a roller coaster ride!自從我開始數據科學之旅以來已經有兩年了 。 男孩 &#xff0c;那可真是坐過山車&#xff01; There were many highs and lows, and…

leetcode 888. 公平的糖果棒交換(set)

愛麗絲和鮑勃有不同大小的糖果棒&#xff1a;A[i] 是愛麗絲擁有的第 i 根糖果棒的大小&#xff0c;B[j] 是鮑勃擁有的第 j 根糖果棒的大小。 因為他們是朋友&#xff0c;所以他們想交換一根糖果棒&#xff0c;這樣交換后&#xff0c;他們都有相同的糖果總量。&#xff08;一個…

如何使用JavaScript檢查輸入是否為空

by Zell Liew由Zell Liew 如何使用JavaScript檢查輸入是否為空 (How to check if an input is empty with JavaScript) Last week, I shared how to check if an input is empty with CSS. Today, let’s talk about the same thing, but with JavaScript.上周&#xff0c;我分…

數學哲學與科學哲學和計算機科學的能動作用,數學哲學與科學哲學和計算機科學的能動作用...

3 數學哲學與計算機科學的能動作用數學哲學對于計算機科學的影響主要表現于以下的事實&#xff1a;一些源于數學哲學(數學基礎研究)的概念和理論在計算機科學的歷史發展中發揮了十分重要的作用。例如&#xff0c;在此可以首先提及(一階)謂詞演算理論&#xff1a;這是由弗雷格(…

AngularDart4.0 指南- 表單

2019獨角獸企業重金招聘Python工程師標準>>> 表單是商業應用程序的主流。您可以使用表單登錄&#xff0c;提交幫助請求&#xff0c;下訂單&#xff0c;預訂航班&#xff0c;安排會議&#xff0c;并執行無數其他數據錄入任務。 在開發表單時&#xff0c;創建一個數據…

(轉載)分享常用的GoLang包工具

分享常用的GoLang包工具 包名 鏈接地址 備注 Machinery異步隊列 https://github.com/RichardKnop/machinery Mqtt通信 github.com/eclipse/paho.mqtt.golang go文檔http://www.eclipse.org/paho/clients/golang/ 微信開發 https://github.com/chanxuehong/wechat fasthttp包 gi…

邁向數據科學的第一步:在Python中支持向量回歸

什么是支持向量回歸&#xff1f; (What is Support Vector Regression?) Support vector regression is a special kind of regression that gives you some sort of buffer or flexibility with the error. How does it do that ? I’m going to explain it to you in simpl…

js 觸發LinkButton點擊事件,執行后臺方法

頁面 <asp:LinkButton ID"lbtButton" runat"server" CssClass"lbtButton" Font-Underline"false" OnClick"lbtButton_Click"> js function clickButton(filePath, fileName){ __doPostBack(lbtButton, ); } 當執行該…

vue 響應式ui_如何在Vue.js中設置響應式UI搜索

vue 響應式uiAre you thinking of building something awesome with one of the popular modern frameworks out there right now, but don’t know how to get started?您是否正在考慮使用當前流行的現代框架之一來構建出色的東西&#xff0c;但不知道如何入門&#xff1f; …

蘭州交通大學計算機科學與技術學院,蘭州交通大學

信息與計算科學專業依托數學和計算機科學與技術兩個一級學科碩士學位授予點&#xff0c;運籌學與控制論、計算機科學與技術兩個省級重點學科&#xff0c;培養理工融合、學科交叉的創新性人才。自2008年以來&#xff0c;承擔國家自然科學基金10余項&#xff0c;發表SCI收錄雜志論…

leetcode 424. 替換后的最長重復字符(滑動窗口)

給你一個僅由大寫英文字母組成的字符串&#xff0c;你可以將任意位置上的字符替換成另外的字符&#xff0c;總共可最多替換 k 次。在執行上述操作后&#xff0c;找到包含重復字母的最長子串的長度。 注意&#xff1a;字符串長度 和 k 不會超過 104。 示例 1&#xff1a; 輸入…

javascript放在head和body的區別(w3c建議放在head標簽中)

JavaScript腳本放在哪里 在HTML body部分中的JavaScripts會在頁面加載的時候被執行。 在HTML head部分中的JavaScripts會在被調用的時候才執行。—————————————————————————— JavaScript應放在哪里 頁面中的JavaScripts會在瀏覽器加載頁面的時候被立即…

jQuery事件整合

一、jQuery事件 1、focus&#xff08;&#xff09;元素獲得焦點 2、blur&#xff08;&#xff09;元素失去焦點 3、change&#xff08;&#xff09; 表單元素的值發生變化&#xff08;可用于驗證用戶名是否存在&#xff09; 4、click&#xff08;&#xff09; 鼠標單擊 5、dbc…

tableau跨庫創建并集_刮擦柏林青年旅舍,并以此建立一個Tableau全景。

tableau跨庫創建并集One of the coolest things about making our personal project is the fact that we can explore topics of our own interest. On my case, I’ve had the chance to backpack around the world for more than a year between 2016–2017, and it was one…

策略模式下表單驗證

策略模式下表單驗證 class Validator {constructor(strategies) {this.cache []}add(value, rules) {if (!rules instanceof Array) throw rules should be Arrayvar self thisfor(var i 0, rule; rule rules[i];) {(function(rule) {var strategyArr rule.strategy.split…

在五分鐘內學習使用Python進行類型轉換

by PALAKOLLU SRI MANIKANTA通過PALAKOLLU SRI MANIKANTA 在五分鐘內學習使用Python進行類型轉換 (Learn typecasting in Python in five minutes) 以非常詳盡的方式介紹了Python中的類型轉換和類型轉換的速成課程 (A crash course on Typecasting and Type conversion in Pyt…

Ajax post HTML 405,Web API Ajax POST向返回 405方法不允許_jquery_開發99編程知識庫

因此&#xff0c;我有一個像這樣的jquery ajax請求&#xff1a;function createLokiAccount(someurl) {var d {"Jurisdiction":17}$.ajax({type:"POST",url:"http://myserver:111/Api/V1/Customers/CreateCustomer/",data: JSON.stringify(d),c…

leetcode 480. 滑動窗口中位數(堆+滑動窗口)

中位數是有序序列最中間的那個數。如果序列的大小是偶數&#xff0c;則沒有最中間的數&#xff1b;此時中位數是最中間的兩個數的平均數。 例如&#xff1a; [2,3,4]&#xff0c;中位數是 3 [2,3]&#xff0c;中位數是 (2 3) / 2 2.5 給你一個數組 nums&#xff0c;有一個大…