基于Comparator對象集合實現(xiàn)多個條件按照優(yōu)先級的比較
更新時間:2021年07月12日 09:25:41 作者:CoderYin
這篇文章主要介紹了基于Comparator對象集合實現(xiàn)多個條件按照優(yōu)先級的比較,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
一、背景介紹
在日常的java開發(fā)中,我們在返回一個對象集合時需要按照對象的某個屬性或者某些屬性進行排序返回給前端進行展示,例如我最近需要返回一個題庫集合,需要先根據(jù)指定時間排序然后根據(jù)創(chuàng)建時間進行排序,在mysql層進行操作比較麻煩而且浪費時間,我們可以通過程序來進行排序。
二、案例代碼
// 實體類
public class People {
private Integer id;
private String name;
private Integer topTime;// 置頂時間
private Integer gmtCreate;// 創(chuàng)建時間
public People(Integer id, String name, Integer topTime, Integer gmtCreate) {
super();
this.id = id;
this.name = name;
this.topTime = topTime;
this.gmtCreate = gmtCreate;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getTopTime() {
return topTime;
}
public void setTopTime(Integer topTime) {
this.topTime = topTime;
}
public Integer getGmtCreate() {
return gmtCreate;
}
public void setGmtCreate(Integer gmtCreate) {
this.gmtCreate = gmtCreate;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", topTime=" + topTime + ", gmtCreate=" + gmtCreate + "]";
}
}
// 排序方法
public class PeopleComparator implements Comparator<People>{
@Override
public int compare(People o1, People o2) {
int result = 0;
// 按照置頂時間排序升序(o1,o2位置互換就是降序)
int topTimeSeq = o2.getTopTime() - o1.getTopTime();
if(topTimeSeq != 0){
result = (topTimeSeq > 0) ? 3 : -1;
}else{
// 按照創(chuàng)建時間排序
topTimeSeq = o2.getGmtCreate() - o1.getGmtCreate();
if(topTimeSeq != 0){
result = (topTimeSeq > 0) ? 2 : -2;
}
}
return result;
}
}
// 測試
public class PeopleTest {
public static void main(String[] args) {
List<People> peopleList = new ArrayList<People>(){
{
add(new People(1,"tom1",0,1));
add(new People(2,"tom2",2,4));
add(new People(3,"tom3",1,3));
add(new People(4,"tom4",0,6));
add(new People(5,"tom5",0,2));
add(new People(6,"tom6",0,5));
}
};
Collections.sort(peopleList,new PeopleComparator());
for(People p:peopleList){
System.out.println(p.toString());
}
}
}
測試結果

Comparator 多條件比較
class Card {
int a;
int b;
public Card(int a, int b) {
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public int getB() {
return b;
}
@Override
public String toString() {
return "Card{" +
"a=" + a +
", b=" + b +
'}';
}
}
public class Main {
public static void main(String[] args) {
List<Card> list = new ArrayList<>();
list.add(new Card(0, 2));
list.add(new Card(1, 1));
list.add(new Card(1, 0));
list.add(new Card(1, 0));
list.add(new Card(2, 0));
System.out.println(list);
System.out.println();
Collections.sort(list, new Comparator<Card>() {
@Override
public int compare(Card c1, Card c2) {
// c1 - c2 升序
// c2 - c1 降序
int res1 = c2.b - c1.b;
int res2 = c2.a - c1.a;
// 當 b相等時比較a, 否則先比較b
return res1 == 0 ? res2 : res1;
}
});
System.out.println(list);
}
}
[Card{a=0, b=2}, Card{a=1, b=1}, Card{a=1, b=0}, Card{a=1, b=0}, Card{a=2, b=0}]
[Card{a=0, b=2}, Card{a=1, b=1}, Card{a=2, b=0}, Card{a=1, b=0}, Card{a=1, b=0}]
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
如何通過RabbitMq實現(xiàn)動態(tài)定時任務詳解
工作中經(jīng)常會有定時任務的需求,常見的做法可以使用Timer、Quartz、Hangfire等組件,這次想嘗試下新的思路,使用RabbitMQ死信隊列的機制來實現(xiàn)定時任務,下面這篇文章主要給大家介紹了關于如何通過RabbitMq實現(xiàn)動態(tài)定時任務的相關資料,需要的朋友可以參考下2022-01-01
springboot添加多數(shù)據(jù)源的方法實例教程
這篇文章主要給大家介紹了關于springboot添加多數(shù)據(jù)源方法的相關資料,在實際開發(fā)中經(jīng)??赡苡龅皆谝粋€應用中可能要訪問多個數(shù)據(jù)庫多的情況,需要的朋友可以參考下2023-09-09
Mybatis中特殊SQL的執(zhí)行的實現(xiàn)示例
本文主要介紹了Mybatis中特殊SQL的執(zhí)行的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07
spring使用@Async注解導致循環(huán)依賴問題異常的排查記錄
這篇文章主要介紹了spring使用@Async注解導致循環(huán)依賴問題異常的排查記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08

