欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java8 stream sort自定義復(fù)雜排序案例

 更新時(shí)間:2020年10月10日 10:48:05   作者:啃過雪糕的兔子  
這篇文章主要介紹了java8 stream sort自定義復(fù)雜排序案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

java 8 自定義排序

需求

今天在項(xiàng)目中遇到個(gè)需求,按照對(duì)象中的三個(gè)屬性進(jìn)行排序。

具體要求:

前提:對(duì)象 Obj [a=a,b=b,c=c]

1、 優(yōu)先級(jí)為a > b > c

2、 a屬性為中文,固定排序規(guī)則為:政府,合作,基金 … …

3、 b的為BigDecimal類型,固定的排序規(guī)則為:降序排序

4、 c為java.util.Date類型,規(guī)則為:降序排序

其實(shí)看這個(gè)需求,第3點(diǎn)和第4點(diǎn)不是什么問題,但是第1點(diǎn),會(huì)考慮下怎么實(shí)現(xiàn)好。

直接上方案吧!

方案一

新建一張排序表,至少要有字段 【名稱—中文名稱(政府、合作、基金等)】【排序編號(hào)—(1、2、3)】,在Obj表中的a字段存排序表的id。

此時(shí)可以直接用sql語句 ORDER BY 排序即可。

優(yōu)點(diǎn):可動(dòng)態(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)就是硬編碼,用戶改排序就得改源碼。對(duì)第二種方案的改進(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ǔ)充知識(shí):java8 stream代替for循環(huán) sort多字段排序 group by多級(jí)排序

我就廢話不多說了,大家還是直接看代碼吧`

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)多字段排序和多級(jí)分組,前面的sort排序會(huì)影響后面的groupBy分組后的list中的排序

mcvo中有有多個(gè)property List ,按商品屬性和銷售屬性區(qū)分將多個(gè)小list聚合為一個(gè)list

mc V o list.stream(). flat Map(mc vo→mcvo.getPropertyList(). stream ())

以上這篇java8 stream sort自定義復(fù)雜排序案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談Java中的參數(shù)傳遞問題

    淺談Java中的參數(shù)傳遞問題

    這篇文章主要介紹了Java中的參數(shù)傳遞問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出

    SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出

    這篇文章主要為大家詳細(xì)介紹了SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • idea更改默認(rèn)緩存位置的步驟詳解

    idea更改默認(rèn)緩存位置的步驟詳解

    在idea使用中,會(huì)產(chǎn)生大量的緩存,一般默認(rèn)存放在C盤里面,會(huì)加大C盤的儲(chǔ)存負(fù)擔(dān),如果C盤足夠大,可以不考慮更改idea的緩存位置,所以本文小編給如何更改idea默認(rèn)緩存位置,文中通過圖文結(jié)合介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • 一篇文章帶你深入了解javaIO基礎(chǔ)

    一篇文章帶你深入了解javaIO基礎(chǔ)

    這篇文章主要介紹了java 基礎(chǔ)知識(shí)之IO總結(jié)的相關(guān)資料,Java中的I/O分為兩種類型,一種是順序讀取,一種是隨機(jī)讀取,需要的朋友可以參考下,希望對(duì)你有幫助
    2021-08-08
  • Spring Boot 2 實(shí)戰(zhàn):自定義啟動(dòng)運(yùn)行邏輯實(shí)例詳解

    Spring Boot 2 實(shí)戰(zhàn):自定義啟動(dòng)運(yùn)行邏輯實(shí)例詳解

    這篇文章主要介紹了Spring Boot 2 實(shí)戰(zhàn):自定義啟動(dòng)運(yùn)行邏輯,結(jié)合實(shí)例形式詳細(xì)分析了Spring Boot 2自定義啟動(dòng)運(yùn)行邏輯詳細(xì)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • IDEA集成JProfiler11可視化工具的詳細(xì)流程(安裝、集成、測試)

    IDEA集成JProfiler11可視化工具的詳細(xì)流程(安裝、集成、測試)

    小編打算在IDEA中集成一下JProfiler11(現(xiàn)在有12版本了)工具,到網(wǎng)上搜都沒有找到合適的,于是自己動(dòng)手寫個(gè),關(guān)于IDEA集成JProfiler11可視化工具(安裝、集成、測試)相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-06-06
  • 詳解如何查看Elasticsearch的Debug日志

    詳解如何查看Elasticsearch的Debug日志

    這篇文章主要為大家介紹了詳解如何查看Elasticsearch的Debug日志,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • OpenTelemetry初識(shí)及調(diào)用鏈Trace詳解

    OpenTelemetry初識(shí)及調(diào)用鏈Trace詳解

    這篇文章主要為為大家介紹了OpenTelemetry初識(shí)及調(diào)用鏈Trace詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Mybatis通過數(shù)據(jù)庫表自動(dòng)生成實(shí)體類和xml映射文件

    Mybatis通過數(shù)據(jù)庫表自動(dòng)生成實(shí)體類和xml映射文件

    這篇文章主要介紹了Mybatis通過數(shù)據(jù)庫表自動(dòng)生成實(shí)體類和xml映射文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot返回html界面的使用示例

    SpringBoot返回html界面的使用示例

    本文主要介紹了SpringBoot返回html界面的使用示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11

最新評(píng)論