Java實(shí)現(xiàn)對象按照其屬性排序的兩種方法示例
本文實(shí)例講述了Java實(shí)現(xiàn)對象按照其屬性排序的兩種方法。分享給大家供大家參考,具體如下:
有時候需要對對象列表或數(shù)組進(jìn)行排序,下面提供兩種簡單方式:
方法一:將要排序的對象類實(shí)現(xiàn)Comparable<>接口。
首先,創(chuàng)建學(xué)生類,我們將根據(jù)學(xué)生成績對學(xué)生進(jìn)行排序:
/** * 學(xué)生類 */ class Student implements Comparable<Student>{ String name; int age; int score; public Student(String name, int age,int score) { this.name = name; this.age = age; this.score = score; } @Override public int compareTo(Studento) { // TODO Auto-generated method stub return this.age - o.age; } }
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Student> students = new ArrayList<>(); students.add(new Student("大銘", 19, 89)); students.add(new Student("來福", 26, 90)); students.add(new Student("倉頡", 23, 70)); students.add(new Student("王磊", 18, 80)); System.out.println("排序前:"); for (Student student : students) { System.out.println("姓名:"+student.name+" 年齡:"+student.age+" 成績:"+student.score); } // 排序 Collections.sort(students); System.out.println("排序后:"); for (Student student : students) { System.out.println("姓名:"+student.name+" 年齡:"+student.age+" 成績:"+student.score); } } }
同理,也可以根據(jù)對象的其他屬性進(jìn)行排序。
方法二:使用Comparator匿名內(nèi)部類實(shí)現(xiàn)。
還是使用同一個例子,按成績將學(xué)生排序:
/** * 學(xué)生類 */ class Student { String name; int age; int score; public Student(String name, int age,int score) { this.name = name; this.age = age; this.score = score; } }
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Student> students = new ArrayList<>(); students.add(new Student("大銘", 19, 89)); students.add(new Student("來福", 26, 90)); students.add(new Student("倉頡", 23, 70)); students.add(new Student("王磊", 18, 80)); System.out.println("排序前:"); for (Student student : students) { System.out.println("姓名:"+student.name+" 年齡:"+student.age+" 成績:"+student.score); } Collections.sort(students,new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub return o1.age-o2.age; } }); System.out.println("排序后:"); for (Student student : students) { System.out.println("姓名:"+student.name+" 年齡:"+student.age+" 成績:"+student.score); } } }
也可以實(shí)現(xiàn)按對象屬性將對象列表排序。
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
- java根據(jù)List內(nèi)對象的屬性排序方法
- java ArrayList集合中的某個對象屬性進(jìn)行排序的實(shí)現(xiàn)代碼
- java中List對象排序通用方法
- java中List對象列表實(shí)現(xiàn)去重或取出及排序的方法
- java 對象數(shù)組排序
- Java按照List內(nèi)存儲的對象的某個字段進(jìn)行排序的實(shí)例
- java實(shí)現(xiàn)List中對象排序的方法
- Java實(shí)體類中Set按照對象的某個字段對set排序
- java實(shí)現(xiàn)ArrayList根據(jù)存儲對象排序功能示例
- java中如何實(shí)現(xiàn)對類的對象進(jìn)行排序
- 如何基于Java實(shí)現(xiàn)對象List排序
- Java實(shí)現(xiàn)儲存對象并按對象某屬性排序的幾種方法示例
相關(guān)文章
Java Swing樹狀組件JTree用法實(shí)例詳解
這篇文章主要介紹了Java Swing樹狀組件JTree用法,結(jié)合具體實(shí)例形式分析了Swing組件JTree構(gòu)成樹狀列表的節(jié)點(diǎn)設(shè)置與事件響應(yīng),以及自定義圖形節(jié)點(diǎn)的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11springboot jasypt2.x與jasypt3.x的使用方式
在軟件開發(fā)中,將配置文件中的敏感信息(如數(shù)據(jù)庫密碼)進(jìn)行加密是保障安全的有效手段,jasypt框架提供了這一功能,支持通過加密工具類或命令行工具生成密文,并通過修改配置文件和啟動參數(shù)的方式使用密文和密鑰,這樣即便配置文件被泄露2024-09-09Java后臺接收數(shù)據(jù)的三種方式(url、form-data與application/json)
本文主要介紹了Java后臺接收數(shù)據(jù)的三種方式(url、form-data與application/json),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07