Java案例使用比較排序器comparator實(shí)現(xiàn)成績排序
需求:用TreeSet
集合存儲多個學(xué)生信息(姓名,語文成績,數(shù)學(xué)成績),并遍歷該集合;要按照總分從高到低進(jìn)行排序
分析:
- 1.創(chuàng)建學(xué)生類 成員變量 姓名,語文成績、數(shù)學(xué)成績;成員方法 求總分;構(gòu)造方法 無參構(gòu)造,帶參構(gòu)造;
get\set
方法 - 2.創(chuàng)建測試類
- 3.創(chuàng)建
TreeSet
集合對對象,并使用內(nèi)部類的方式重寫compare
方法
要定好排序規(guī)則,主要條件按照總分從高到底排序,在總分相同的情況下按照語文成績排序,在兩者都相同的情況下判斷姓名是否相同,相同就不存儲,不相同存進(jìn)來,按照姓名字母進(jìn)行排序
- 4.創(chuàng)建學(xué)生對象,并使用帶參構(gòu)造添加學(xué)生數(shù)據(jù)
- 5.使用
add
方法將學(xué)生數(shù)據(jù)加入到TreeSet集合中 - 6.進(jìn)行遍歷
代碼實(shí)現(xiàn):
Student類
public class Student { ? //成員變量 ? private String name; ? private int YWscore; ? private int YYscore; ? ? //構(gòu)造方法 ? public Student(){} ? ? public Student(String name, int YWscore, int YYscore) { ? ? ? this.name = name; ? ? ? this.YWscore = YWscore; ? ? ? this.YYscore = YYscore; ? } ? //get/set方法 ? ? public String getName() { ? ? ? return name; ? } ? ? public void setName(String name) { ? ? ? this.name = name; ? } ? ? public int getYWscore() { ? ? ? return YWscore; ? } ? ? public void setYWscore(int YWscore) { ? ? ? this.YWscore = YWscore; ? } ? ? public int getYYscore() { ? ? ? return YYscore; ? } ? ? public void setYYscore(int YYscore) { ? ? ? this.YYscore = YYscore; ? } ? //定義求總成績方法 ? public int getSum(){ ? ? ? int sum=YWscore+YYscore; ? ? ? return sum; ? } } ?
測試類
public class StudentDemo { ? public static void main(String[] args) { ? ? ? //創(chuàng)建TreeSet集合對象 ? ? ? TreeSet<Student>ts=new TreeSet<Student>(new Comparator<Student>() { ? ? ? ? ? @Override ? ? ? ? ? public int compare(Student s1, Student s2) { // ? ? ? ? ? ? ? return 0; ? ? ? ? ? ? ? int num=s2.getSum()-s1.getSum();//要從高到底排序 ? ? ? ? ? ? ? int num1= num==0?s1.getYWscore()-s2.getYWscore():num;//當(dāng)總分相同時按照語文成績排序 ? ? ? ? ? ? ? int num2= num1==0?s1.getName().compareTo(s2.getName()):num1; ? ? ? ? ? ? ? return num2; ? ? ? ? ? } ? ? ? }); ? ? ? //創(chuàng)建學(xué)生對象 ? ? ? Student s1=new Student("張三",56,66); ? ? ? Student s2=new Student("張四",70,69); ? ? ? Student s3=new Student("張五",80,76); ? ? ? Student s4=new Student("張六",66,96); ? ? ? Student s5=new Student("張七",66,96); ? ? ? ts.add(s5); ? ? ? ts.add(s1); ? ? ? ts.add(s2); ? ? ? ts.add(s3); ? ? ? ts.add(s4); ? ? ? //遍歷 ? ? ? for (Student ss:ts){ ? ? ? ? ? System.out.println(ss.getName()+","+ss.getYWscore()+","+ss.getYYscore()+","+ss.getSum()); ? ? ? } ? } }
到此這篇關(guān)于Java案例使用比較排序器comparator實(shí)現(xiàn)成績排序的文章就介紹到這了,更多相關(guān)comparator實(shí)現(xiàn)成績排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Kafka是什么及如何使用SpringBoot對接Kafka(最新推薦)
這篇文章主要介紹了Kafka是什么,以及如何使用SpringBoot對接Kafka,今天我們通過一個Demo講解了在SpringBoot中如何對接Kafka,也介紹了下關(guān)鍵類?KafkaTemplate,需要的朋友可以參考下2023-11-11關(guān)于Java項(xiàng)目讀取resources資源文件路徑的那點(diǎn)事
這篇文章主要介紹了關(guān)于Java項(xiàng)目讀取resources資源文件路徑的那點(diǎn)事,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07jvm垃圾回收GC調(diào)優(yōu)基礎(chǔ)原理分析
談到調(diào)優(yōu),這一定是針對特定場景、特定目的的事情, 對于 GC 調(diào)優(yōu)來說,首先就需要清楚調(diào)優(yōu)的目標(biāo)是什么?從性能的角度看,通常關(guān)注三個方面,內(nèi)存占用(footprint)、延時(latency)和吞吐量(throughput)2022-01-01idea全局設(shè)置Maven配置的實(shí)現(xiàn)步驟
本文主要介紹了idea全局設(shè)置Maven配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Java面試題沖刺第十九天--數(shù)據(jù)庫(4)
這篇文章主要為大家分享了最有價值的三道關(guān)于數(shù)據(jù)庫的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-08-08Java實(shí)現(xiàn)的求解經(jīng)典羅馬數(shù)字和阿拉伯?dāng)?shù)字相互轉(zhuǎn)換問題示例
這篇文章主要介紹了Java實(shí)現(xiàn)的求解經(jīng)典羅馬數(shù)字和阿拉伯?dāng)?shù)字相互轉(zhuǎn)換問題,涉及java輸入輸出及字符串、數(shù)組的遍歷與轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2018-04-04