Stream流排序數(shù)組和List?詳解
一、對象單字段排序
List<People> peopleList = Lists.newArrayList();
peopleList.add(new People(1, "小王", 5));
peopleList.add(new People(1, "小李", 4));
peopleList.add(new People(2, "小張", 3));
peopleList.add(new People(2, "小皇", 2));
peopleList.add(new People(2, "小劉", 1));
//單字段排序
peopleList = peopleList.stream().sorted(Comparator.comparing(People::getJgId)).collect(Collectors.toList());
log.info(peopleList.toString());
//這里是根據(jù)userId 進行排序——降序排序 reversed()
peopleList = peopleList.stream().sorted(Comparator.comparing(People::getJgId).reversed()).collect(Collectors.toList());
log.info(peopleList.toString());
二、多字段排序
List<People> peopleList = Lists.newArrayList();
peopleList.add(new People(1, "小王", 5));
peopleList.add(new People(1, "小李", 4));
peopleList.add(new People(2, "小張", 3));
peopleList.add(new People(2, "小皇", 2));
peopleList.add(new People(2, "小劉", 1));
//這里是根據(jù)Id及jgId進行聯(lián)合升序排序
peopleList = peopleList.stream().sorted(Comparator.comparing(People::getId).thenComparing(People::getJgId)).collect(Collectors.toList());
log.info(peopleList.toString());
//下面兩個結(jié)果都是以Id降序jgId升序排序的結(jié)果,但是查詢方式不同
//先以id升序,升序結(jié)果進行id降序,再進行jgId升序
peopleList = peopleList.stream().sorted(Comparator.comparing(People::getId).reversed().thenComparing(People::getJgId)).collect(Collectors.toList());
log.info(peopleList.toString());
//先以id降序,再進行jgId升序 **推薦使用該種方式**
peopleList = peopleList.stream().sorted(Comparator.comparing(People::getId,Comparator.reverseOrder()).thenComparing(People::getJgId)).collect(Collectors.toList());
log.info(peopleList.toString());
//先以id升序,再進行jgId降序
peopleList = peopleList.stream().sorted(Comparator.comparing(People::getId).thenComparing(People::getJgId,Comparator.reverseOrder())).collect(Collectors.toList());
log.info(peopleList.toString());
三、數(shù)組排序以及List<Integer>排序
先把數(shù)組轉(zhuǎn)換成List對象再進行排序

到此這篇關(guān)于Stream流排序數(shù)組和List 詳解的文章就介紹到這了,更多相關(guān)Stream List 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea如何debug看springsecurity的過濾器順序
這篇文章主要介紹了idea如何debug看springsecurity的過濾器順序,文中通過圖文結(jié)合的方式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04
IDEA2023.3.4開啟SpringBoot項目的熱部署(圖文)
本文使用的開發(fā)工具是idea,使用的是springboot框架開發(fā)的項目,配置熱部署,可以提高開發(fā)效率,文中通過圖文介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
一篇文章帶你了解java Object根類中關(guān)于toString,equals的方法
這篇文章主要介紹了Object類toString()和equals()方法使用解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-09-09
Spring3 整合MyBatis3 配置多數(shù)據(jù)源動態(tài)選擇SqlSessionFactory詳細教程
這篇文章主要介紹了Spring3 整合MyBatis3 配置多數(shù)據(jù)源動態(tài)選擇SqlSessionFactory詳細教程,需要的朋友可以參考下2017-04-04
mybatis實現(xiàn)獲取入?yún)⑹荓ist和Map的取值
這篇文章主要介紹了mybatis實現(xiàn)獲取入?yún)⑹荓ist和Map的取值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06

