java根據(jù)List內(nèi)對象的屬性排序方法
更新時間:2018年01月03日 14:36:25 作者:Clannad_汐
下面小編就為大家分享一篇java根據(jù)List內(nèi)對象的屬性排序方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
方法一:實現(xiàn)Comparator接口,并重寫compare方法
實體類代碼:
import java.util.Comparator; /** * 學(xué)生類 方法一 * 實現(xiàn)Comparator接口 * 并重寫compare方法 * @author liaot * */ public class Student implements Comparator<Student>{ private String name; //姓名 private int age; //年齡 //重寫 比較方法 本次例子定義為按年齡比較 @Override public int compare(Student o1, Student o2) { if(o1.getAge() > o2.getAge()){ return 1; }else{ return -1; } } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
測試類:
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] args) { //初始化四個不同的學(xué)生 Student stu1 = new Student("路人甲", 20); Student stu2 = new Student("路人已", 18); Student stu3 = new Student("路人丙", 16); Student stu4 = new Student("路人丁", 19); //新建List把學(xué)生加進List List<Student> stuList = new ArrayList<>(); stuList.add(stu1); stuList.add(stu2); stuList.add(stu3); stuList.add(stu4); System.out.println("排序前:====="); for(Student stu :stuList){ System.out.println("姓名:"+stu.getName() +" 年齡"+stu.getAge()); } //排序 Collections.sort(stuList, stu1); //第一個參數(shù)為List 第二個參數(shù)為對象的一個實例 System.out.println("排序后:====="); for(Student stu :stuList){ System.out.println("姓名:"+stu.getName() +" 年齡"+stu.getAge()); } } }
運行結(jié)果:
方法二:實現(xiàn)Comparable接口 并重寫compareTo方法
/** * 學(xué)生類 方法二 實現(xiàn)Comparable接口 并重寫compareTo方法 * * @author liaot * */ public class Student2 implements Comparable<Student2> { private String name; // 姓名 private int age; // 年齡 // 重寫 比較方法 本次例子定義為按年齡比較 @Override public int compareTo(Student2 stu) { if (this.age > stu.getAge()) { return 1; } else { return -1; } } public Student2(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
測試類
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main2 { public static void main(String[] args) { //初始化四個不同的學(xué)生 Student2 stu1 = new Student2("路人甲", 20); Student2 stu2 = new Student2("路人已", 18); Student2 stu3 = new Student2("路人丙", 16); Student2 stu4 = new Student2("路人丁", 19); //新建List把學(xué)生加進List List<Student2> stuList = new ArrayList<>(); stuList.add(stu1); stuList.add(stu2); stuList.add(stu3); stuList.add(stu4); System.out.println("排序前:====="); for(Student2 stu :stuList){ System.out.println("姓名:"+stu.getName() +" 年齡"+stu.getAge()); } //排序 Collections.sort(stuList); //只有一個參數(shù)參數(shù)為List System.out.println("排序后:====="); for(Student2 stu :stuList){ System.out.println("姓名:"+stu.getName() +" 年齡"+stu.getAge()); } } }
運行結(jié)果
三、總結(jié):兩種方式寫法和用法上的區(qū)別:
以上這篇java根據(jù)List內(nèi)對象的屬性排序方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java動態(tài)代理Proxy應(yīng)用和底層源碼詳細分析
Java動態(tài)代理是一種在運行時生成代理類的機制,用于代替手動編寫代理類的過程,這篇文章主要給大家介紹了關(guān)于Java動態(tài)代理Proxy應(yīng)用和底層源碼詳細分析的相關(guān)資料,需要的朋友可以參考下2024-03-03DecimalFormat數(shù)字格式化 0和# 的區(qū)別及說明
這篇文章主要介紹了DecimalFormat數(shù)字格式化 0和# 的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10