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

Java List集合排序?qū)崿F(xiàn)方法解析

 更新時(shí)間:2019年12月27日 09:14:01   作者:糖不甜,鹽不咸  
這篇文章主要介紹了Java List集合排序?qū)崿F(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Java List集合排序?qū)崿F(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1.使用 Collections 工具類中的 sort() 方法

參數(shù)不同:

void sort(List list) 在自定義類User里面實(shí)現(xiàn)Comparable<User>接口,并重寫抽象方法compareTo(Student o);

void sort(List list, Comparator c) 第二個(gè)參數(shù)為了省事,可以直接使用匿名內(nèi)部類

public class User implements Comparable<User>{ 
   
  private int score; 
   
  private int age; 
   
  public User(int score, int age){ 
    super(); 
    this.score = score; 
    this.age = age; 
  } 
 
  public int getScore() { 
    return score; 
  } 
 
  public void setScore(int score) { 
    this.score = score; 
  } 
 
  public int getAge() { 
    return age; 
  } 
 
  public void setAge(int age) { 
    this.age = age; 
  } 
 
  @Override 
  public int compareTo(User o) { 
    int i = this.getAge() - o.getAge();//先按照年齡排序 
    if(i == 0){ 
      return this.score - o.getScore();//如果年齡相等了再用分?jǐn)?shù)進(jìn)行排序 
    } 
    return i; 
  } 
   
} 
 
public static void main(String[] args) { 
    List<User> users = new ArrayList<User>(); 
    users.add(new User(78, 26)); 
    users.add(new User(67, 23)); 
    users.add(new User(34, 56)); 
    users.add(new User(55, 23)); 
    Collections.sort(users); 
    for(User user : users){ 
      System.out.println(user.getScore() + "," + user.getAge()); 
    } 
}
public class Students { 
   
  private int age; 
  private int score; 
   
  public Students(int age, int score){ 
    super(); 
    this.age = age; 
    this.score = score; 
  } 
   
  public int getAge() { 
    return age; 
  } 
  public void setAge(int age) { 
    this.age = age; 
  } 
  public int getScore() { 
    return score; 
  } 
  public void setScore(int score) { 
    this.score = score; 
  } 
} 
public static void main(String[] args) { 
    List<Students> students = new ArrayList<Students>(); 
    students.add(new Students(23, 100)); 
    students.add(new Students(27, 98)); 
    students.add(new Students(29, 99)); 
    students.add(new Students(29, 98)); 
    students.add(new Students(22, 89)); 
    Collections.sort(students, new Comparator<Students>() { 
 
      @Override 
      public int compare(Students o1, Students o2) { 
        int i = o1.getScore() - o2.getScore(); 
        if(i == 0){ 
          return o1.getAge() - o2.getAge(); 
        } 
        return i; 
      } 
    }); 
    for(Students stu : students){ 
      System.out.println("score:" + stu.getScore() + ":age" + stu.getAge()); 
    } 
}

2.直接使用list.sort()方法,傳入實(shí)現(xiàn)Comparator接口的實(shí)現(xiàn)類的實(shí)例,為了省事直接傳入匿名內(nèi)部類

public class Students {

  private int age;
  private int score;

  public Students(int age, int score){
    this.age = age;
    this.score = score;
  }

  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public int getScore() {
    return score;
  }
  public void setScore(int score) {
    this.score = score;
  }
}
public static void main(String[] args) {
  List<Students> students = new ArrayList<Students>();
  students.add(new Students(23, 100));
  students.add(new Students(27, 98));
  students.add(new Students(29, 99));
  students.add(new Students(29, 98));
  students.add(new Students(22, 89));

  students.sort(new Comparator<Students>() {
    @Override
    public int compare(Students o1, Students o2) {
      int i = o1.getScore() - o2.getScore();
      if (i == 0) {
        return o1.getAge() - o2.getAge();
      }
      return i;
    }
  });

  for (Students stu : students) {
    System.out.println("score:" + stu.getScore() + ":age" + stu.getAge());
  }
}

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

相關(guān)文章

  • springMVC+ajax實(shí)現(xiàn)文件上傳且?guī)нM(jìn)度條實(shí)例

    springMVC+ajax實(shí)現(xiàn)文件上傳且?guī)нM(jìn)度條實(shí)例

    本篇文章主要介紹了springMVC+ajax實(shí)現(xiàn)文件上傳且?guī)нM(jìn)度條實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • Java 在PPT中添加文本和圖片超鏈接的實(shí)現(xiàn)方法

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

    這篇文章主要介紹了Java 在PPT中添加文本和圖片超鏈接的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Spring中@Value讀取properties作為map或list的操作

    Spring中@Value讀取properties作為map或list的操作

    這篇文章主要介紹了Spring中@Value讀取properties作為map或list的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java對(duì)接支付寶支付接口簡(jiǎn)單步驟記錄

    java對(duì)接支付寶支付接口簡(jiǎn)單步驟記錄

    最近項(xiàng)目APP需要接入微信、支付寶支付功能,在分配開發(fā)任務(wù)時(shí),聽說微信支付接口比支付寶支付接口要難實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于java對(duì)接支付寶支付接口的簡(jiǎn)單步驟,需要的朋友可以參考下
    2024-05-05
  • java非遞歸實(shí)現(xiàn)之二叉樹的前中后序遍歷詳解

    java非遞歸實(shí)現(xiàn)之二叉樹的前中后序遍歷詳解

    樹的遍歷順序大體分為三種:前序遍歷(先根遍歷、先序遍歷),中序遍歷(中根遍歷),后序遍歷(后根遍歷),本文將給大家詳細(xì)的介紹,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值
    2021-09-09
  • Java淺析代碼塊與構(gòu)造塊及靜態(tài)塊三者之間的關(guān)系

    Java淺析代碼塊與構(gòu)造塊及靜態(tài)塊三者之間的關(guān)系

    所謂代碼塊是指用"{}"括起來的一段代碼,根據(jù)其位置和聲明的不同,可以分為普通代碼塊、構(gòu)造塊、靜態(tài)塊、和同步代碼塊。如果在代碼塊前加上synchronized關(guān)鍵字,則此代碼塊就成為同步代碼塊
    2022-07-07
  • 最新評(píng)論