java根據(jù)List內(nèi)對(duì)象的屬性排序方法
方法一:實(shí)現(xiàn)Comparator接口,并重寫compare方法
實(shí)體類代碼:
import java.util.Comparator;
/**
* 學(xué)生類 方法一
* 實(shí)現(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;
}
}
測(cè)試類:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
//初始化四個(gè)不同的學(xué)生
Student stu1 = new Student("路人甲", 20);
Student stu2 = new Student("路人已", 18);
Student stu3 = new Student("路人丙", 16);
Student stu4 = new Student("路人丁", 19);
//新建List把學(xué)生加進(jìn)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); //第一個(gè)參數(shù)為L(zhǎng)ist 第二個(gè)參數(shù)為對(duì)象的一個(gè)實(shí)例
System.out.println("排序后:=====");
for(Student stu :stuList){
System.out.println("姓名:"+stu.getName() +" 年齡"+stu.getAge());
}
}
}
運(yùn)行結(jié)果:

方法二:實(shí)現(xiàn)Comparable接口 并重寫compareTo方法
/**
* 學(xué)生類 方法二 實(shí)現(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;
}
}
測(cè)試類
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main2 {
public static void main(String[] args) {
//初始化四個(gè)不同的學(xué)生
Student2 stu1 = new Student2("路人甲", 20);
Student2 stu2 = new Student2("路人已", 18);
Student2 stu3 = new Student2("路人丙", 16);
Student2 stu4 = new Student2("路人丁", 19);
//新建List把學(xué)生加進(jìn)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); //只有一個(gè)參數(shù)參數(shù)為L(zhǎng)ist
System.out.println("排序后:=====");
for(Student2 stu :stuList){
System.out.println("姓名:"+stu.getName() +" 年齡"+stu.getAge());
}
}
}
運(yùn)行結(jié)果

三、總結(jié):兩種方式寫法和用法上的區(qū)別:

以上這篇java根據(jù)List內(nèi)對(duì)象的屬性排序方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
HDFS的Java API的訪問(wèn)方式實(shí)例代碼
這篇文章主要介紹了HDFS的Java API的訪問(wèn)方式實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
解決報(bào)錯(cuò):java.lang.IllegalStateException: Failed to&nb
在項(xiàng)目開發(fā)中,可能會(huì)遇到Elasticsearch啟動(dòng)報(bào)錯(cuò)的問(wèn)題,原因可能包括版本不一致、端口配置錯(cuò)誤、配置文件不匹配及服務(wù)未啟動(dòng)等,解決方法包括檢查進(jìn)程、重啟服務(wù)等,這些經(jīng)驗(yàn)可以幫助開發(fā)者快速定位問(wèn)題并解決,保證項(xiàng)目順利運(yùn)行2024-10-10
Spring Data MongoDB 數(shù)據(jù)庫(kù)批量操作的方法
在項(xiàng)目開發(fā)中經(jīng)常會(huì)批量插入數(shù)據(jù)和更新數(shù)據(jù)的操作,這篇文章主要介紹了Spring Data MongoDB 數(shù)據(jù)庫(kù)批量操作的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-11-11
springboot+vue制作后臺(tái)管理系統(tǒng)項(xiàng)目
本文詳細(xì)介紹了后臺(tái)管理使用springboot+vue制作,以分步驟、圖文的形式詳細(xì)講解,大家有需要的可以參考參考2021-08-08
IDEA教程創(chuàng)建SpringBoot前后端分離項(xiàng)目示例圖解
在使用spring、mybatis等框架時(shí),配置文件很復(fù)雜,有時(shí)復(fù)雜的讓人想放棄Java,使用C#。springboot出現(xiàn)這一切問(wèn)題就都不是問(wèn)題2021-10-10
Spring中的策略模式簡(jiǎn)單實(shí)現(xiàn)與使用分析
這篇文章主要介紹了Spring中的策略模式簡(jiǎn)單實(shí)現(xiàn)與使用分析,去初始化時(shí)除了?initMultipartResolver(上傳文件)沒(méi)有獲取?Properties?defaultStrategies;默認(rèn)策略,其他的八大件都會(huì)使用到策略模式,需要的朋友可以參考下2024-01-01

