Java針對ArrayList自訂排序的2種實現方法,javaarraylist

來源:互聯網
上載者:User

Java針對ArrayList自訂排序的2種實現方法,javaarraylist

本文執行個體講述了Java針對ArrayList自訂排序的2種實現方法。分享給大家供大家參考,具體如下:

Java中實現對list的自訂排序主要通過兩種方式

1)讓需要進行排序的對象的類實現Comparable介面,重寫compareTo(T o)方法,在其中定義定序,那麼就可以直接調用Collections.sort()來排序對象數組

public class Student implements Comparable{  private int id;  private int age;  private int height;  private String name;  public Student(int id, String name, int age, int height) {    this.id = id;    this.name = name;    this.age = age;    this.height = height;  }  public int getId() {    return id;  }  public int getAge() {    return age;  }  public int getHeight() {    return height;  }  public String getName() {    return name;  }  public void setId(int id) {    this.id = id;  }  public void setAge(int age) {    this.age = age;  }  public void setName(String name) {    this.name = name;  }  public void setHeight(int height) {    this.height = height;  }  @Override  public int compareTo(Object o) {    Student s = (Student) o;    if (this.age > s.age) {      return 1;    }    else if (this.age < s.age) {      return -1;    }    else {      if (this.height >= s.height) {        return 1;      }      else {        return -1;      }    }  }}

測試類別:

import java.util.*;public class Test {  public static void printData(List<Student> list) {    for (Student student : list) {      System.out.println("學號:" + student.getId() + " 姓名:" + student.getName() + " 年齡" + student.getAge() + " 身高:" + student.getHeight());    }  }  public static void main(String[] args) {    List<Student> list = new ArrayList<>();    list.add(new Student(1, "A", 20, 180));    list.add(new Student(2, "B", 21, 175));    list.add(new Student(3, "C", 22, 190));    list.add(new Student(4, "D", 21, 170));    list.add(new Student(5, "E", 20, 185));    System.out.println("before sorted");    printData(list);    Collections.sort(list);    System.out.println("after age and height sorted");    printData(list);  }}

結果:

before sorted學號:1 姓名:A 年齡20 身高:180學號:2 姓名:B 年齡21 身高:175學號:3 姓名:C 年齡22 身高:190學號:4 姓名:D 年齡21 身高:170學號:5 姓名:E 年齡20 身高:185after age and height sorted學號:1 姓名:A 年齡20 身高:180學號:5 姓名:E 年齡20 身高:185學號:4 姓名:D 年齡21 身高:170學號:2 姓名:B 年齡21 身高:175學號:3 姓名:C 年齡22 身高:190

2)實現比較子介面Comparator,重寫compare方法,直接當做參數傳進sort中

public class Student {  private int id;  private int age;  private int height;  private String name;  public Student(int id, String name, int age, int height) {    this.id = id;    this.name = name;    this.age = age;    this.height = height;  }  public int getId() {    return id;  }  public int getAge() {    return age;  }  public int getHeight() {    return height;  }  public String getName() {    return name;  }  public void setId(int id) {    this.id = id;  }  public void setAge(int age) {    this.age = age;  }  public void setName(String name) {    this.name = name;  }  public void setHeight(int height) {    this.height = height;  }}

測試類別:

import java.util.*;public class Test {  public static void printData(List<Student> list) {    for (Student student : list) {      System.out.println("學號:" + student.getId() + " 姓名:" + student.getName() + " 年齡" + student.getAge() + " 身高:" + student.getHeight());    }  }  public static void main(String[] args) {    List<Student> list = new ArrayList<>();    list.add(new Student(1, "A", 20, 180));    list.add(new Student(2, "B", 21, 175));    list.add(new Student(3, "C", 22, 190));    list.add(new Student(4, "D", 21, 170));    list.add(new Student(5, "E", 20, 185));    System.out.println("before sorted");    printData(list);    Collections.sort(list, new Comparator<Student>() {      @Override      public int compare(Student o1, Student o2) {        if(o1.getAge() >= o2.getAge()) {          return 1;        }        else {          return -1;        }      }    });    System.out.println("after age sorted");    printData(list);    Collections.sort(list, new Comparator<Student>() {      @Override      public int compare(Student o1, Student o2) {        if(o1.getAge() > o2.getAge()) {          return 1;        }        else if (o1.getAge() < o2.getAge()){          return -1;        }        else {          if (o1.getHeight() >= o2.getHeight()) {            return 1;          }          else {            return -1;          }        }      }    });    System.out.println("after age and height sorted");    printData(list);  }}

輸出結果:

before sorted學號:1 姓名:A 年齡20 身高:180學號:2 姓名:B 年齡21 身高:175學號:3 姓名:C 年齡22 身高:190學號:4 姓名:D 年齡21 身高:170學號:5 姓名:E 年齡20 身高:185after age sorted學號:1 姓名:A 年齡20 身高:180學號:5 姓名:E 年齡20 身高:185學號:2 姓名:B 年齡21 身高:175學號:4 姓名:D 年齡21 身高:170學號:3 姓名:C 年齡22 身高:190after age and height sorted學號:1 姓名:A 年齡20 身高:180學號:5 姓名:E 年齡20 身高:185學號:4 姓名:D 年齡21 身高:170學號:2 姓名:B 年齡21 身高:175學號:3 姓名:C 年齡22 身高:190

單從上面的例子可以看出排序是穩定的,去看了下java的Collections.sort的原始碼,確實是基於穩定的歸併排序實現的,內部還做了最佳化,叫TimSort。(關於TimSort還可參考https://baike.baidu.com/item/TimSort?fr=aladdin)

PS:這裡再為大家推薦一款關於排序的示範工具供大家參考:

線上動畫示範插入/選擇/冒泡/歸併/希爾/快速排序演算法過程工具:
http://tools.jb51.net/aideddesign/paixu_ys

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.