java使用lambda表達(dá)式多條件排序方式
使用java的lambda表達(dá)式多條件排序
這里多條件是指同時(shí)生效
先把我的對象擺上
@Data @AllArgsConstructor @ToString public class Student { private String name; private String age; private Integer id; private Integer score; }
然后再準(zhǔn)備好排序的數(shù)據(jù)
List<Student> studentList = new ArrayList<>(); studentList.add(new Student("1", "222", 5, 8)); studentList.add(new Student("2", "111", 3, 9)); studentList.add(new Student("3", "224", 4, 6)); studentList.add(new Student("4", "333", 3, 11)); studentList.add(new Student("4", "444", 5, 6)); System.out.println(studentList);
這里的構(gòu)造按照實(shí)體里的屬性先后定的位置,1條件代表id,2條件代表score
有以下集中場景
System.out.println("例一--------------1升2升------------"); studentList = studentList.stream().sorted(Comparator.comparing(Student::getId).thenComparing(Student::getScore)).collect(Collectors.toList()); for (Student s : studentList) { System.out.println(s); } System.out.println("例二--------------1升2降------------"); studentList = studentList.stream().sorted(Comparator.comparing(Student::getId).reversed().thenComparing(Student::getScore).reversed()).collect(Collectors.toList()); for (Student s : studentList) { System.out.println(s); } System.out.println("例三--------------2降1升------------"); studentList = studentList.stream().sorted(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getId)).collect(Collectors.toList()); for (Student s : studentList) { System.out.println(s); } System.out.println("例四--------------1降2升------------"); studentList = studentList.stream().sorted(Comparator.comparing(Student::getId).reversed().thenComparing(Student::getScore)).collect(Collectors.toList()); for (Student s : studentList) { System.out.println(s); } System.out.println("例五--------------1降2降------------"); studentList = studentList.stream().sorted(Comparator.comparing(Student::getId).thenComparing(Student::getScore).reversed()).collect(Collectors.toList()); for (Student s : studentList) { System.out.println(s); }
結(jié)果:
--------------1升2升------------
Student(name=2, age=111, id=3, score=9)
Student(name=4, age=333, id=3, score=11)
Student(name=3, age=224, id=4, score=6)
Student(name=4, age=444, id=5, score=6)
Student(name=1, age=222, id=5, score=8)
--------------1升2降------------
Student(name=4, age=333, id=3, score=11)
Student(name=2, age=111, id=3, score=9)
Student(name=3, age=224, id=4, score=6)
Student(name=1, age=222, id=5, score=8)
Student(name=4, age=444, id=5, score=6)
--------------2降1升------------
Student(name=4, age=333, id=3, score=11)
Student(name=2, age=111, id=3, score=9)
Student(name=1, age=222, id=5, score=8)
Student(name=3, age=224, id=4, score=6)
Student(name=4, age=444, id=5, score=6)
--------------1降2升------------
Student(name=4, age=444, id=5, score=6)
Student(name=1, age=222, id=5, score=8)
Student(name=3, age=224, id=4, score=6)
Student(name=2, age=111, id=3, score=9)
Student(name=4, age=333, id=3, score=11)
--------------1降2降------------
Student(name=1, age=222, id=5, score=8)
Student(name=4, age=444, id=5, score=6)
Student(name=3, age=224, id=4, score=6)
Student(name=4, age=333, id=3, score=11)
Student(name=2, age=111, id=3, score=9)
在這里注意
1升2降和2升1降有區(qū)別的,誰在前誰優(yōu)先級高;第一、三、四個(gè)例子都好理解,第二、五需要思考一下,這里的reversed()妙用。
reversed()是把列表倒過來,所以1升2降,就是先把1降,然后再把1、2都倒置過來,這樣就是1升2降了,所以1降2降就是再后面都倒置過來。
使用lambda表達(dá)式排序還是很nice啊,節(jié)省不少操作,但數(shù)據(jù)量最好不要太大。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC 重新定向redirect請求中攜帶數(shù)據(jù)方式
這篇文章主要介紹了SpringMVC 重新定向redirect請求中攜帶數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12使用@value注解取不到application.xml配置文件中的值問題
這篇文章主要介紹了使用@value注解取不到application.xml配置文件中的值問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Springmvc自定義參數(shù)轉(zhuǎn)換實(shí)現(xiàn)代碼解析
這篇文章主要介紹了Springmvc自定義參數(shù)轉(zhuǎn)換實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-072020macOS Big Sur配置Java開發(fā)環(huán)境之jdk安裝過程
這篇文章主要介紹了2020macOS Big Sur配置Java開發(fā)環(huán)境之jdk安裝,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02使用SpringBoot與Thrift實(shí)現(xiàn)RPC通信的方式詳解
在微服務(wù)架構(gòu)的世界里,服務(wù)間的通信機(jī)制選擇成為了關(guān)鍵決策之一,RPC因其簡潔、高效的特點(diǎn)備受青睞,本文將詳細(xì)探討如何利用Spring?Boot和Thrift框架構(gòu)建RPC通信,讓讀者理解其內(nèi)在原理及實(shí)現(xiàn)方式,需要的朋友可以參考下2023-10-10詳解Jenkins 實(shí)現(xiàn)Gitlab事件自動觸發(fā)Jenkins構(gòu)建及釘釘消息推送
這篇文章主要介紹了Jenkins 實(shí)現(xiàn)Gitlab事件自動觸發(fā)Jenkins構(gòu)建及釘釘消息推送,應(yīng)該會對大家學(xué)習(xí)Jenkins有所啟發(fā)2021-04-04SpringBoot的WebSocket實(shí)現(xiàn)單聊群聊
這篇文章主要為大家詳細(xì)介紹了SpringBoot的WebSocket實(shí)現(xiàn)單聊群聊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02