Java使用Collections.sort()排序的方法
Java中Collections.sort()的使用
public class Student implements Comparable<Student> { private int id; private int age; private String name; public Student(int id, int age, String name) { this.id = id; this.age = age; this.name = name; } @Override public int compareTo(Student o) { //降序 //return o.age - this.age; //升序 return this.age - o.age; } @Override public String toString() { return "Student{" + "id=" + id + ", age=" + age + ", name='" + name + '\'' + '}'; } }
返回值 | 含義 |
---|---|
負(fù)整數(shù) | 當(dāng)前對象的值 < 比較對象的值 , 位置排在前 |
零 | 當(dāng)前對象的值 = 比較對象的值 , 位置不變 |
正整數(shù) | 當(dāng)前對象的值 > 比較對象的值 , 位置排在后 |
測試代碼:
public static void main(String args[]){ List<Student> list = new ArrayList<>(); list.add(new Student(1,25,"關(guān)羽")); list.add(new Student(2,21,"張飛")); list.add(new Student(3,18,"劉備")); list.add(new Student(4,32,"袁紹")); list.add(new Student(5,36,"趙云")); list.add(new Student(6,16,"曹操")); System.out.println("排序前:"); for (Student student : list) { System.out.println(student.toString()); } //使用默認(rèn)排序 Collections.sort(list); System.out.println("默認(rèn)排序后:"); for (Student student : list) { System.out.println(student.toString()); } }
輸出日志:
排序前: Student{id=1, age=25, name='關(guān)羽'} Student{id=2, age=21, name='張飛'} Student{id=3, age=18, name='劉備'} Student{id=4, age=32, name='袁紹'} Student{id=5, age=36, name='趙云'} Student{id=6, age=16, name='曹操'} 默認(rèn)排序后: Student{id=6, age=16, name='曹操'} Student{id=3, age=18, name='劉備'} Student{id=2, age=21, name='張飛'} Student{id=1, age=25, name='關(guān)羽'} Student{id=4, age=32, name='袁紹'} Student{id=5, age=36, name='趙云'}
比較器的使用
- Collections.sort(list,Comparator<T>);
- list.sort(Comparator<T>);
//自定義排序1 Collections.sort(list, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getId() - o2.getId(); } });
compare(Student o1, Student o2) 方法的返回值跟 Comparable<> 接口中的 compareTo(Student o) 方法 返回值意思相同。另一種寫法如下:
//自定義排序2 list.sort(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getId() - o2.getId(); } });
輸出日志:
排序前: Student{id=1, age=25, name='關(guān)羽'} Student{id=2, age=21, name='張飛'} Student{id=3, age=18, name='劉備'} Student{id=4, age=32, name='袁紹'} Student{id=5, age=36, name='趙云'} Student{id=6, age=16, name='曹操'} 自定義排序后: Student{id=1, age=25, name='關(guān)羽'} Student{id=2, age=21, name='張飛'} Student{id=3, age=18, name='劉備'} Student{id=4, age=32, name='袁紹'} Student{id=5, age=36, name='趙云'} Student{id=6, age=16, name='曹操'}
以上所述是小編給大家介紹的Java使用Collections.sort()排序的方法,希望對大家有所幫助。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring中ApplicationEventPublisher發(fā)布訂閱模式的實現(xiàn)
本文主要介紹了Spring中ApplicationEventPublisher發(fā)布訂閱模式的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07使用google.kaptcha來生成圖片驗證碼的實現(xiàn)方法
這篇文章主要介紹了使用google.kaptcha來生成圖片驗證碼的實現(xiàn)方法,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法
下面小編就為大家?guī)硪黄狫ava 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02MyBatis insert語句返回主鍵和selectKey標(biāo)簽方式
這篇文章主要介紹了MyBatis insert語句返回主鍵和selectKey標(biāo)簽方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Mybatis多數(shù)據(jù)源切換實現(xiàn)代碼
這篇文章主要介紹了Mybatis多數(shù)據(jù)源切換實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10