本文實例講述了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程序設計有所幫助。
新聞熱點
疑難解答
圖片精選