Java針對(duì)ArrayList自定義排序的2種實(shí)現(xiàn)方法
本文實(shí)例講述了Java針對(duì)ArrayList自定義排序的2種實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
Java中實(shí)現(xiàn)對(duì)list的自定義排序主要通過兩種方式
1)讓需要進(jìn)行排序的對(duì)象的類實(shí)現(xiàn)Comparable接口,重寫compareTo(T o)方法,在其中定義排序規(guī)則,那么就可以直接調(diào)用Collections.sort()來排序?qū)ο髷?shù)組
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;
}
}
}
}
測(cè)試類:
import java.util.*;
public class Test {
public static void printData(List<Student> list) {
for (Student student : list) {
System.out.println("學(xué)號(hào):" + 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);
}
}
結(jié)果:
before sorted 學(xué)號(hào):1 姓名:A 年齡20 身高:180 學(xué)號(hào):2 姓名:B 年齡21 身高:175 學(xué)號(hào):3 姓名:C 年齡22 身高:190 學(xué)號(hào):4 姓名:D 年齡21 身高:170 學(xué)號(hào):5 姓名:E 年齡20 身高:185 after age and height sorted 學(xué)號(hào):1 姓名:A 年齡20 身高:180 學(xué)號(hào):5 姓名:E 年齡20 身高:185 學(xué)號(hào):4 姓名:D 年齡21 身高:170 學(xué)號(hào):2 姓名:B 年齡21 身高:175 學(xué)號(hào):3 姓名:C 年齡22 身高:190
2)實(shí)現(xiàn)比較器接口Comparator,重寫compare方法,直接當(dāng)做參數(shù)傳進(jìn)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;
}
}
測(cè)試類:
import java.util.*;
public class Test {
public static void printData(List<Student> list) {
for (Student student : list) {
System.out.println("學(xué)號(hào):" + 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);
}
}
輸出結(jié)果:
before sorted 學(xué)號(hào):1 姓名:A 年齡20 身高:180 學(xué)號(hào):2 姓名:B 年齡21 身高:175 學(xué)號(hào):3 姓名:C 年齡22 身高:190 學(xué)號(hào):4 姓名:D 年齡21 身高:170 學(xué)號(hào):5 姓名:E 年齡20 身高:185 after age sorted 學(xué)號(hào):1 姓名:A 年齡20 身高:180 學(xué)號(hào):5 姓名:E 年齡20 身高:185 學(xué)號(hào):2 姓名:B 年齡21 身高:175 學(xué)號(hào):4 姓名:D 年齡21 身高:170 學(xué)號(hào):3 姓名:C 年齡22 身高:190 after age and height sorted 學(xué)號(hào):1 姓名:A 年齡20 身高:180 學(xué)號(hào):5 姓名:E 年齡20 身高:185 學(xué)號(hào):4 姓名:D 年齡21 身高:170 學(xué)號(hào):2 姓名:B 年齡21 身高:175 學(xué)號(hào):3 姓名:C 年齡22 身高:190
單從上面的例子可以看出排序是穩(wěn)定的,去看了下java的Collections.sort的源代碼,確實(shí)是基于穩(wěn)定的歸并排序?qū)崿F(xiàn)的,內(nèi)部還做了優(yōu)化,叫TimSort。(關(guān)于TimSort還可參考https://baike.baidu.com/item/TimSort?fr=aladdin)
PS:這里再為大家推薦一款關(guān)于排序的演示工具供大家參考:
在線動(dòng)畫演示插入/選擇/冒泡/歸并/希爾/快速排序算法過程工具:
http://tools.jb51.net/aideddesign/paixu_ys
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring與Struts整合之使用自動(dòng)裝配操作示例
這篇文章主要介紹了Spring與Struts整合之使用自動(dòng)裝配操作,結(jié)合實(shí)例形式詳細(xì)分析了Spring與Struts整合使用自動(dòng)裝配具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-01-01
SpringShell命令行之交互式Shell應(yīng)用開發(fā)方式
本文將深入探討Spring Shell的核心特性、實(shí)現(xiàn)方式及應(yīng)用場(chǎng)景,幫助開發(fā)者掌握這一強(qiáng)大工具,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Java Agent入門學(xué)習(xí)之動(dòng)態(tài)修改代碼
這篇文章主要給大家分享了Java Agent入門學(xué)習(xí)之動(dòng)態(tài)修改代碼的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07
SpringBoot發(fā)送短信驗(yàn)證碼的實(shí)例
第三方短信發(fā)送平臺(tái)有很多種,各個(gè)平臺(tái)有各自的優(yōu)缺點(diǎn),在選擇的時(shí)候可以根據(jù)自己的具體實(shí)際情況定奪,本文主要介紹了SpringBoot發(fā)送短信驗(yàn)證碼的實(shí)例,感興趣的可以了解一下2022-02-02
SpringBoot+ResponseBodyEmitter實(shí)時(shí)異步流式推送的實(shí)現(xiàn)
本文主要介紹了SpringBoot+ResponseBodyEmitter實(shí)時(shí)異步流式推送的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-11-11
springMVC發(fā)送郵件的簡(jiǎn)單實(shí)現(xiàn)
本篇文章主要介紹了springMVC發(fā)送郵件的簡(jiǎn)單實(shí)現(xiàn) ,主要是利用利用javax.mail發(fā)送郵件,圖片與附件都可發(fā)送,有興趣的可以了解一下2017-04-04
IntelliJ IDEA 設(shè)置代碼提示或自動(dòng)補(bǔ)全的快捷鍵功能
這篇文章主要介紹了IntelliJ IDEA 設(shè)置代碼提示或自動(dòng)補(bǔ)全的快捷鍵功能,需要的朋友可以參考下2018-03-03

