java8 stream sort自定義復(fù)雜排序案例
java 8 自定義排序
需求
今天在項(xiàng)目中遇到個需求,按照對象中的三個屬性進(jìn)行排序。
具體要求:
前提:對象 Obj [a=a,b=b,c=c]
1、 優(yōu)先級為a > b > c
2、 a屬性為中文,固定排序規(guī)則為:政府,合作,基金 … …
3、 b的為BigDecimal類型,固定的排序規(guī)則為:降序排序
4、 c為java.util.Date類型,規(guī)則為:降序排序
其實(shí)看這個需求,第3點(diǎn)和第4點(diǎn)不是什么問題,但是第1點(diǎn),會考慮下怎么實(shí)現(xiàn)好。
直接上方案吧!
方案一
新建一張排序表,至少要有字段 【名稱—中文名稱(政府、合作、基金等)】【排序編號—(1、2、3)】,在Obj表中的a字段存排序表的id。
此時可以直接用sql語句 ORDER BY 排序即可。
優(yōu)點(diǎn):可動態(tài)配置。
方案二
完全用java代碼操作,和sql無關(guān),上代碼:
Obj.java 和 Sort.java
package TestSort; import java.math.BigDecimal; public class Obj { private String name; private BigDecimal price; public Obj(String name, BigDecimal price){ this.name = name; this.price = price; } public Obj(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } @Override public String toString() { return "Obj [name=" + name + ", price=" + price + "]"; } }
package TestSort; import java.math.BigDecimal; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class Sort { public static void main(String[] args) { List<Obj> list = Arrays.asList( new Obj("政府", null), new Obj("政府", new BigDecimal("1216.23")), new Obj("商業(yè)", new BigDecimal("123.23")), new Obj("PPD", new BigDecimal("123.23")), new Obj("合作", new BigDecimal("127.23")), new Obj("合作", new BigDecimal("125.23")), new Obj("咨詢", null), new Obj(null, null) ); /*Comparator<Obj> byName = Comparator.comparing(Obj::getName).reversed(); Comparator<Obj> finalByPrice= byName.thenComparing(Obj::getPrice,Comparator.nullsFirst(BigDecimal::compareTo)).reversed(); List<Obj> result = list.stream().filter(new Predicate<Obj>() { @Override public boolean test(Obj obj) { if(obj.getName() == null && obj.getPrice() ==null){ return false; } return true; } }).sorted(finalByPrice).collect(Collectors.toList());*/ List<Obj> result = list.stream().sorted( //先按照name排序(模擬需求的a屬性排序) Comparator.comparing(Obj::getName,(x,y)->{ if(x == null && y != null){ return 1; }else if(x !=null && y == null){ return -1; }else if(x == null && y == null){ return -1; }else if("PPD".equals(x) || "PPD".equals(y)){ if(x.equals(y)){ return 0; }else if("PPD".equals(x)){ return -1; }else{ return 1; } }else if("合作".equals(x) || "合作".equals(y)){ if(x.equals(y)){ return 0; }else if("合作".equals(x)){ return -1; }else{ return 1; } }else if("政府".equals(x) || "政府".equals(y)){ if(x.equals(y)){ return 0; }else if("政府".equals(x)){ return -1; }else{ return 1; } } return 0; }) //再按照其他字段排序,要考慮null(模擬需求b和c字段排序) .thenComparing(Comparator.comparing(Obj::getPrice, Comparator.nullsFirst(BigDecimal::compareTo)).reversed() )).collect(Collectors.toList()); System.out.println(result); System.out.println(result.size()); } }
方案二的缺點(diǎn)就是硬編碼,用戶改排序就得改源碼。對第二種方案的改進(jìn):
package TestSort; import java.math.BigDecimal; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class Sort { public static void main(String[] args) { List<Obj> list = Arrays.asList( new Obj("政府", null), new Obj("政府", new BigDecimal("1216.23")), new Obj("商業(yè)", new BigDecimal("123.23")), new Obj("PPD", new BigDecimal("123.23")), new Obj("合作", new BigDecimal("127.23")), new Obj("合作", new BigDecimal("125.23")), new Obj("咨詢", null), new Obj(null, null) ); /*Comparator<Obj> byName = Comparator.comparing(Obj::getName).reversed(); Comparator<Obj> finalByPrice= byName.thenComparing(Obj::getPrice,Comparator.nullsFirst(BigDecimal::compareTo)).reversed(); List<Obj> result = list.stream().filter(new Predicate<Obj>() { @Override public boolean test(Obj obj) { if(obj.getName() == null && obj.getPrice() ==null){ return false; } return true; } }).sorted(finalByPrice).collect(Collectors.toList());*/ //此處模擬從數(shù)據(jù)讀取配置到list List<String> sortList = Arrays.asList("PPD","政府","合作"); list.stream().sorted( Comparator.comparing(Obj::getName,(x,y)->{ if(x == null && y != null){ return 1; }else if(x !=null && y == null){ return -1; }else if(x == null && y == null){ return -1; }else{ //按照讀取的list順序排序 for(String sort : sortList){ if(sort.equals(x) || sort.equals(y)){ if(x.equals(y)){ return 0; }else if(sort.equals(x)){ return -1; }else{ return 1; } } } return 0; } }).thenComparing(Comparator.comparing(Obj::getPrice,Comparator.nullsFirst(BigDecimal::compareTo)).reversed()) ).collect(Collectors.toList()).forEach(System.out::println);; } }
補(bǔ)充知識:java8 stream代替for循環(huán) sort多字段排序 group by多級排序
我就廢話不多說了,大家還是直接看代碼吧`
List<MacTicket> list = new ArrayList(); Category ctg= new Category(); ctg.setType(1);//0商品,1銷售規(guī)格 ctg.setSort(2); ctg.setInheritFlag(0);//0繼承屬性1非繼承屬性 ctg.setValueSort(1) Category ctg1= new Category(); ctg1.setType(0);//0商品,1銷售規(guī)格 ctg1.setSort(1); ctg1.setInheritFlag(1);//0繼承屬性1非繼承屬性 ctg1.setValueSort(2) list.add(ctg); list.add(ctg1); lista.stream().sorted(Comparator.comparing(Category::getSort()).thenComparing(Category::getValueSort())).collect(Collectors.groupingBy(fetchGroupInheritKey(mtg)),Collectors.groupingBy(fetchGroupTypeKey(mtg))); private String fetchGroupInheritKey(Category ctg){ if(inheritFlag==0){ return "inheritList"; }else if(inheritFlag==1){ return "ownList"; }else{ return ""; } }
使用Comparator實(shí)現(xiàn)多字段排序和多級分組,前面的sort排序會影響后面的groupBy分組后的list中的排序
mcvo中有有多個property List ,按商品屬性和銷售屬性區(qū)分將多個小list聚合為一個list
mc V o list.stream(). flat Map(mc vo→mcvo.getPropertyList(). stream ())
以上這篇java8 stream sort自定義復(fù)雜排序案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05Spring Boot 2 實(shí)戰(zhàn):自定義啟動運(yùn)行邏輯實(shí)例詳解
這篇文章主要介紹了Spring Boot 2 實(shí)戰(zhàn):自定義啟動運(yùn)行邏輯,結(jié)合實(shí)例形式詳細(xì)分析了Spring Boot 2自定義啟動運(yùn)行邏輯詳細(xì)操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-05-05IDEA集成JProfiler11可視化工具的詳細(xì)流程(安裝、集成、測試)
小編打算在IDEA中集成一下JProfiler11(現(xiàn)在有12版本了)工具,到網(wǎng)上搜都沒有找到合適的,于是自己動手寫個,關(guān)于IDEA集成JProfiler11可視化工具(安裝、集成、測試)相關(guān)知識感興趣的朋友一起看看吧2021-06-06OpenTelemetry初識及調(diào)用鏈Trace詳解
這篇文章主要為為大家介紹了OpenTelemetry初識及調(diào)用鏈Trace詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Mybatis通過數(shù)據(jù)庫表自動生成實(shí)體類和xml映射文件
這篇文章主要介紹了Mybatis通過數(shù)據(jù)庫表自動生成實(shí)體類和xml映射文件的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07