如何基于Java實(shí)現(xiàn)對(duì)象List排序
這篇文章主要介紹了如何基于Java實(shí)現(xiàn)對(duì)象List排序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
按照對(duì)象中的某個(gè)屬性,對(duì)對(duì)象List進(jìn)行排序。
以初唐四杰的成績(jī)排名為例,對(duì)詩(shī)人進(jìn)行排序。
Java實(shí)現(xiàn)如下:
1、詩(shī)人(Poet)類結(jié)構(gòu),定義如下:
/**
* Created by Miracle Luna on 2020/1/11
*/
public class Poet {
private String name;
private Double score;
public Poet(String name, Double score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
@Override
public String toString() {
return "Poet{" +
"name='" + name + '\'' +
", score=" + score +
'}';
}
}
2、詩(shī)人按照成績(jī)排名,代碼如下:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* Created by Miracle Luna on 2020/1/11
*/
public class PoetSort {
public static void main(String[] args) {
List<Poet> poetList = new ArrayList<Poet>();
Poet poet1 = new Poet("楊炯", 94.0);
poetList.add(poet1);
Poet poet2 = new Poet("盧照鄰", 92.5);
poetList.add(poet2);
Poet poet3 = new Poet("駱賓王", 95.0);
poetList.add(poet3);
Poet poet4 = new Poet("王勃", 99.5);
poetList.add(poet4);
// 初始順序
System.out.println("==> 初始順序如下:");
poetList.forEach(poet -> System.out.println(poet.toString()));
// 按照分?jǐn)?shù)排名(從高到低)
poetList.sort(new Comparator<Poet>() {
@Override
public int compare(Poet poet1, Poet poet2) {
Double score1 = poet1.getScore();
Double score2 = poet2.getScore();
return score2.compareTo(score1);
}
});
System.out.println("\n==> 按照分?jǐn)?shù)排名(從高到低)如下:");
poetList.forEach(poet -> System.out.println(poet.toString()));
// 按照分?jǐn)?shù)排名(從低到高)
poetList.sort(new Comparator<Poet>() {
@Override
public int compare(Poet poet1, Poet poet2) {
Double score1 = poet1.getScore();
Double score2 = poet2.getScore();
return score1.compareTo(score2);
}
});
System.out.println("\n==> 按照分?jǐn)?shù)排名(從低到高)如下:");
poetList.forEach(poet -> System.out.println(poet.toString()));
}
}
3、運(yùn)行結(jié)果如下:
==> 初始順序如下:
Poet{name='楊炯', score=94.0}
Poet{name='盧照鄰', score=92.5}
Poet{name='駱賓王', score=95.0}
Poet{name='王勃', score=99.5}
==> 按照分?jǐn)?shù)排名(從高到低)如下:
Poet{name='王勃', score=99.5}
Poet{name='駱賓王', score=95.0}
Poet{name='楊炯', score=94.0}
Poet{name='盧照鄰', score=92.5}
==> 按照分?jǐn)?shù)排名(從低到高)如下:
Poet{name='盧照鄰', score=92.5}
Poet{name='楊炯', score=94.0}
Poet{name='駱賓王', score=95.0}
Poet{name='王勃', score=99.5}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java讀取txt文件和寫入txt文件的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄狫ava讀取txt文件和寫入txt文件的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
springboot發(fā)布dubbo服務(wù)注冊(cè)到nacos實(shí)現(xiàn)方式
這篇文章主要介紹了springboot發(fā)布dubbo服務(wù)注冊(cè)到nacos實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
使用google.kaptcha來生成圖片驗(yàn)證碼的實(shí)現(xiàn)方法
這篇文章主要介紹了使用google.kaptcha來生成圖片驗(yàn)證碼的實(shí)現(xiàn)方法,非常不錯(cuò)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
java中Calendar與Date類型互相轉(zhuǎn)換的方法
這篇文章主要介紹了java中Calendar與Date類型互相轉(zhuǎn)換的方法,Calendar與Date類型是我們?nèi)粘i_發(fā)中常用的兩種數(shù)據(jù)類型,它們用于不同的場(chǎng)景,兩者具有不同的方法,接下來通過實(shí)例給大家詳解,需要的朋友可以參考下2022-09-09
Spring?Security實(shí)現(xiàn)基于RBAC的權(quán)限表達(dá)式動(dòng)態(tài)訪問控制的操作方法
這篇文章主要介紹了Spring?Security實(shí)現(xiàn)基于RBAC的權(quán)限表達(dá)式動(dòng)態(tài)訪問控制,資源權(quán)限表達(dá)式動(dòng)態(tài)權(quán)限控制在Spring Security也是可以實(shí)現(xiàn)的,首先開啟方法級(jí)別的注解安全控制,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04

