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

Java8 Comparator排序方法實例詳解

 更新時間:2019年12月27日 15:31:08   作者:天涯過者  
這篇文章主要介紹了Java8 Comparator排序方法實例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了Java8 Comparator排序方法實例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

Java8 中 Comparator 接口提供了一些靜態(tài)方法,可以方便于我們進(jìn)行排序操作,下面通過例子講解下如何使用

對整數(shù)列表排序(升序)

List<Integer> list = Arrays.asList(1, 4, 2, 6, 2, 8);
list.sort(Comparator.naturalOrder());
System.out.println(list);

對整數(shù)列表排序(降序)

List<Integer> list = Arrays.asList(1, 4, 2, 6, 2, 8);
list.sort(Comparator.reverseOrder());
System.out.println(list);

根據(jù)對象屬性(年齡)進(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ù)對象屬性(價格、速度)進(jìn)行排序,需要注意的是,排序有先后之分,不同的順序會導(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));
    // 先以價格(升序)、后再速度(升序)
  list.sort(Comparator.comparingInt(Computer::getPrice).thenComparingInt(Computer::getSpeed));
    // 先以速度(降序)、后再價格(升序)
   list.sort(Comparator.comparingInt(Computer::getSpeed).reversed().thenComparingInt(Computer::getPrice));
    // 先以價格(降序)、后再速度(降序)
    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 方法
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?Security+JWT簡述(附源碼)

    Spring?Security+JWT簡述(附源碼)

    SpringSecurity是一個強大的可高度定制的認(rèn)證和授權(quán)框架,對于Spring應(yīng)用來說它是一套Web安全標(biāo)準(zhǔn),下面這篇文章主要給大家介紹了關(guān)于Spring?Security+JWT簡述的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • java中URLencode、URLdecode及Base64加解密轉(zhuǎn)換

    java中URLencode、URLdecode及Base64加解密轉(zhuǎn)換

    本文主要介紹了java中URLencode、URLdecode及Base64加解密轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • 基于Spring中的事務(wù)@Transactional細(xì)節(jié)與易錯點、幻讀

    基于Spring中的事務(wù)@Transactional細(xì)節(jié)與易錯點、幻讀

    這篇文章主要介紹了基于Spring中的事務(wù)@Transactional細(xì)節(jié)與易錯點、幻讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java處理時間格式CST和GMT轉(zhuǎn)換方法示例

    Java處理時間格式CST和GMT轉(zhuǎn)換方法示例

    這篇文章主要給大家介紹了關(guān)于Java處理時間格式CST和GMT轉(zhuǎn)換方法的相關(guān)資料,相信很多小伙伴在時間格式轉(zhuǎn)換的時候非常頭疼,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • 淺談Spring Security 對于靜態(tài)資源的攔截與放行

    淺談Spring Security 對于靜態(tài)資源的攔截與放行

    這篇文章主要介紹了淺談Spring Security 對于靜態(tài)資源的攔截與放行,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • SpringBoot實現(xiàn)整合微信支付方法詳解

    SpringBoot實現(xiàn)整合微信支付方法詳解

    這篇文章主要介紹了SpringBoot實現(xiàn)整合微信支付的過程詳解,文中的示例代碼對我們的工作或?qū)W習(xí)有一定的幫助,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下
    2021-12-12
  • 詳解Spring Boot微服務(wù)如何集成fescar解決分布式事務(wù)問題

    詳解Spring Boot微服務(wù)如何集成fescar解決分布式事務(wù)問題

    這篇文章主要介紹了詳解Spring Boot微服務(wù)如何集成fescar解決分布式事務(wù)問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • 關(guān)于Java中你所不知道的Integer詳解

    關(guān)于Java中你所不知道的Integer詳解

    這篇文章主要給大家介紹了關(guān)于Java中你所不知道的一些關(guān)于Integer的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • Java 在PPT中添加文本和圖片超鏈接的實現(xiàn)方法

    Java 在PPT中添加文本和圖片超鏈接的實現(xiàn)方法

    這篇文章主要介紹了Java 在PPT中添加文本和圖片超鏈接的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • windows下java環(huán)境變量的設(shè)置方法

    windows下java環(huán)境變量的設(shè)置方法

    在“系統(tǒng)變量”中,設(shè)置3項屬性,JAVA_HOME,PATH,CLASSPATH(大小寫無所謂),若已存在則點擊“編輯”,不存在則點擊“新建”
    2013-09-09

最新評論