Java案例使用比較排序器comparator實現(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)行遍歷
代碼實現(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實現(xiàn)成績排序的文章就介紹到這了,更多相關(guān)comparator實現(xiàn)成績排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Date與String的相互轉(zhuǎn)換詳解
這篇文章主要介紹了Java Date與String的相互轉(zhuǎn)換詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02
SpringBoot中使用MyBatis-Plus實現(xiàn)分頁接口的詳細(xì)教程
MyBatis-Plus是一個MyBatis的增強(qiáng)工具,在MyBatis的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生,在SpringBoot項目中使用MyBatis-Plus可以大大簡化分頁邏輯的編寫,本文將介紹如何在 SpringBoot項目中使用MyBatis-Plus實現(xiàn)分頁接口2024-03-03
JavaScript 與 Java 區(qū)別介紹 學(xué)java怎么樣
JavaScript 是一種嵌入式腳本文件,直接插入網(wǎng)頁,有瀏覽器一邊解釋一邊執(zhí)行。而java 語言不一樣,他必須在JAVA虛擬機(jī)上運(yùn)行。而且事先需要進(jìn)行編譯。接下來腳本之家小編給大家揭曉js與java區(qū)別,感興趣的朋友一起看看吧2016-09-09
RestTemplate接口調(diào)用神器常見用法匯總
這篇文章主要介紹了RestTemplate接口調(diào)用神器常見用法匯總,通過案例代碼詳細(xì)介紹RestTemplate接口調(diào)用神器常見用法,需要的朋友可以參考下2022-07-07

