基于Comparator對(duì)象集合實(shí)現(xiàn)多個(gè)條件按照優(yōu)先級(jí)的比較
一、背景介紹
在日常的java開(kāi)發(fā)中,我們?cè)诜祷匾粋€(gè)對(duì)象集合時(shí)需要按照對(duì)象的某個(gè)屬性或者某些屬性進(jìn)行排序返回給前端進(jìn)行展示,例如我最近需要返回一個(gè)題庫(kù)集合,需要先根據(jù)指定時(shí)間排序然后根據(jù)創(chuàng)建時(shí)間進(jìn)行排序,在mysql層進(jìn)行操作比較麻煩而且浪費(fèi)時(shí)間,我們可以通過(guò)程序來(lái)進(jìn)行排序。
二、案例代碼
// 實(shí)體類 public class People { private Integer id; private String name; private Integer topTime;// 置頂時(shí)間 private Integer gmtCreate;// 創(chuàng)建時(shí)間 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; // 按照置頂時(shí)間排序升序(o1,o2位置互換就是降序) int topTimeSeq = o2.getTopTime() - o1.getTopTime(); if(topTimeSeq != 0){ result = (topTimeSeq > 0) ? 3 : -1; }else{ // 按照創(chuàng)建時(shí)間排序 topTimeSeq = o2.getGmtCreate() - o1.getGmtCreate(); if(topTimeSeq != 0){ result = (topTimeSeq > 0) ? 2 : -2; } } return result; } }
// 測(cè)試 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()); } } }
測(cè)試結(jié)果
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; // 當(dāng) b相等時(shí)比較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}]
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何通過(guò)RabbitMq實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)詳解
工作中經(jīng)常會(huì)有定時(shí)任務(wù)的需求,常見(jiàn)的做法可以使用Timer、Quartz、Hangfire等組件,這次想嘗試下新的思路,使用RabbitMQ死信隊(duì)列的機(jī)制來(lái)實(shí)現(xiàn)定時(shí)任務(wù),下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)RabbitMq實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)的相關(guān)資料,需要的朋友可以參考下2022-01-01java 基礎(chǔ)之JavaBean屬性命名規(guī)范問(wèn)題
這篇文章主要介紹了java 基礎(chǔ)之JavaBean屬性命名規(guī)范問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-05-05springboot添加多數(shù)據(jù)源的方法實(shí)例教程
這篇文章主要給大家介紹了關(guān)于springboot添加多數(shù)據(jù)源方法的相關(guān)資料,在實(shí)際開(kāi)發(fā)中經(jīng)??赡苡龅皆谝粋€(gè)應(yīng)用中可能要訪問(wèn)多個(gè)數(shù)據(jù)庫(kù)多的情況,需要的朋友可以參考下2023-09-09java外賣(mài)訂餐系統(tǒng)小項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了java外賣(mài)訂餐系統(tǒng)小項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Mybatis中特殊SQL的執(zhí)行的實(shí)現(xiàn)示例
本文主要介紹了Mybatis中特殊SQL的執(zhí)行的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07圖解Spring框架的設(shè)計(jì)理念與設(shè)計(jì)模式
這篇文章主要通過(guò)多圖詳細(xì)解釋Spring框架的設(shè)計(jì)理念與設(shè)計(jì)模式,需要的朋友可以參考下2015-08-08spring使用@Async注解導(dǎo)致循環(huán)依賴問(wèn)題異常的排查記錄
這篇文章主要介紹了spring使用@Async注解導(dǎo)致循環(huán)依賴問(wèn)題異常的排查記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08