国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java針對ArrayList自定義排序的2種實現方法

2024-07-13 10:16:42
字體:
來源:轉載
供稿:網友

本文實例講述了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)

希望本文所述對大家java程序設計有所幫助。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洪江市| 平原县| 盱眙县| 嘉定区| 青冈县| 丹东市| 鹤峰县| 敦煌市| 巨野县| 富平县| 嘉黎县| 湾仔区| 克东县| 涪陵区| 九龙坡区| 普兰县| 德令哈市| 尚义县| 阿克苏市| 铜陵市| 巴林左旗| 土默特右旗| 绥宁县| 云浮市| 宽甸| 奉化市| 武宣县| 阿巴嘎旗| 垦利县| 平度市| 烟台市| 合肥市| 屯昌县| 个旧市| 延川县| 侯马市| 玉林市| 鸡西市| 屯留县| 安龙县| 陕西省|