Java8 Comparator排序方法實(shí)例詳解
這篇文章主要介紹了Java8 Comparator排序方法實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
Java8 中 Comparator 接口提供了一些靜態(tài)方法,可以方便于我們進(jìn)行排序操作,下面通過例子講解下如何使用
對(duì)整數(shù)列表排序(升序)
List<Integer> list = Arrays.asList(1, 4, 2, 6, 2, 8); list.sort(Comparator.naturalOrder()); System.out.println(list);
對(duì)整數(shù)列表排序(降序)
List<Integer> list = Arrays.asList(1, 4, 2, 6, 2, 8); list.sort(Comparator.reverseOrder()); System.out.println(list);
根據(jù)對(duì)象屬性(年齡)進(jìn)行排序
public class Test { public static void main(String[] args) { List<Person> personList = new ArrayList<>(); personList.add(new Person("a", 2)); personList.add(new Person("b", 4)); personList.add(new Person("c", 7)); // 升序 personList.sort(Comparator.comparingInt(Person::getAge)); // 降序 personList.sort(Comparator.comparingInt(Person::getAge).reversed()); System.out.println(personList); } public static class Person { private String name; private Integer age; public Person(String name, Integer age) { this.name = name; this.age = age; } public Integer getAge() { return age; } // ... toString 方法 } }
根據(jù)對(duì)象屬性(價(jià)格、速度)進(jìn)行排序,需要注意的是,排序有先后之分,不同的順序會(huì)導(dǎo)致不同的結(jié)果
public class Test { public static void main(String[] args) { List<Computer> list = new ArrayList<>(); list.add(new Computer("xiaomi",4000,6)); list.add(new Computer("sony",5000,4)); list.add(new Computer("dell",4000,5)); list.add(new Computer("mac",6000,8)); list.add(new Computer("micro",5000,6)); // 先以價(jià)格(升序)、后再速度(升序) list.sort(Comparator.comparingInt(Computer::getPrice).thenComparingInt(Computer::getSpeed)); // 先以速度(降序)、后再價(jià)格(升序) list.sort(Comparator.comparingInt(Computer::getSpeed).reversed().thenComparingInt(Computer::getPrice)); // 先以價(jià)格(降序)、后再速度(降序) list.sort(Comparator.comparingInt(Computer::getPrice).thenComparingInt(Computer::getSpeed).reversed()); System.out.println(list); } public static class Computer { private String name; private Integer price; private Integer speed; public Computer(String name, Integer price, Integer speed) { this.name = name; this.price = price; this.speed = speed; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public Integer getSpeed() { return speed; } public void setSpeed(Integer speed) { this.speed = speed; } // ... toString 方法 } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Security+JWT簡(jiǎn)述(附源碼)
SpringSecurity是一個(gè)強(qiáng)大的可高度定制的認(rèn)證和授權(quán)框架,對(duì)于Spring應(yīng)用來說它是一套Web安全標(biāo)準(zhǔn),下面這篇文章主要給大家介紹了關(guān)于Spring?Security+JWT簡(jiǎn)述的相關(guān)資料,需要的朋友可以參考下2023-04-04java中URLencode、URLdecode及Base64加解密轉(zhuǎn)換
本文主要介紹了java中URLencode、URLdecode及Base64加解密轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01基于Spring中的事務(wù)@Transactional細(xì)節(jié)與易錯(cuò)點(diǎn)、幻讀
這篇文章主要介紹了基于Spring中的事務(wù)@Transactional細(xì)節(jié)與易錯(cuò)點(diǎn)、幻讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java處理時(shí)間格式CST和GMT轉(zhuǎn)換方法示例
這篇文章主要給大家介紹了關(guān)于Java處理時(shí)間格式CST和GMT轉(zhuǎn)換方法的相關(guān)資料,相信很多小伙伴在時(shí)間格式轉(zhuǎn)換的時(shí)候非常頭疼,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09淺談Spring Security 對(duì)于靜態(tài)資源的攔截與放行
這篇文章主要介紹了淺談Spring Security 對(duì)于靜態(tài)資源的攔截與放行,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08SpringBoot實(shí)現(xiàn)整合微信支付方法詳解
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)整合微信支付的過程詳解,文中的示例代碼對(duì)我們的工作或?qū)W習(xí)有一定的幫助,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2021-12-12詳解Spring Boot微服務(wù)如何集成fescar解決分布式事務(wù)問題
這篇文章主要介紹了詳解Spring Boot微服務(wù)如何集成fescar解決分布式事務(wù)問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01Java 在PPT中添加文本和圖片超鏈接的實(shí)現(xiàn)方法
這篇文章主要介紹了Java 在PPT中添加文本和圖片超鏈接的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05windows下java環(huán)境變量的設(shè)置方法
在“系統(tǒng)變量”中,設(shè)置3項(xiàng)屬性,JAVA_HOME,PATH,CLASSPATH(大小寫無所謂),若已存在則點(diǎn)擊“編輯”,不存在則點(diǎn)擊“新建”2013-09-09