Java使用Collections.sort()排序的方法
更新時間:2021年12月31日 09:09:49 作者:飛渡浮舟~~
這篇文章介紹了Java使用Collections.sort()排序的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
Java中Collections.sort()的使用
在日常開發(fā)中,很多時候都需要對一些數據進行排序的操作。然而那些數據一般都是放在一個集合中如:Map ,Set ,List 等集合中。他們都提共了一個排序方法 sort(),要對數據排序直接使用這個方法就行,但是要保證集合中的對象是 可比較的。
怎么讓一個對象是 可比較的,那就需要該對象實現 Comparable<T> 接口啦。然后重寫里面的
compareTo()方法。我們可以看到Java中很多類都是實現類這個接口的 如:Integer,Long 等等。。。
假設我們有一個學生類,默認需要按學生的年齡字段 age 進行排序 代碼如下:
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 + '\'' + '}'; } }
這里說一下重寫的 public int compareTo(Student o){} 這個方法,它返回三種 int 類型的值: 負整數,零 ,正整數。
返回值 | 含義 |
---|---|
負整數 | 當前對象的值 < 比較對象的值 , 位置排在前 |
零 | 當前對象的值 = 比較對象的值 , 位置不變 |
正整數 | 當前對象的值 > 比較對象的值 , 位置排在后 |
測試代碼:
public static void main(String args[]){ List<Student> list = new ArrayList<>(); list.add(new Student(1,25,"關羽")); 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()); } //使用默認排序 Collections.sort(list); System.out.println("默認排序后:"); for (Student student : list) { System.out.println(student.toString()); } }
輸出日志:
排序前: Student{id=1, age=25, name='關羽'} 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=6, age=16, name='曹操'} Student{id=3, age=18, name='劉備'} Student{id=2, age=21, name='張飛'} Student{id=1, age=25, name='關羽'} Student{id=4, age=32, name='袁紹'} Student{id=5, age=36, name='趙云'}
比較器的使用
這個時候需求又來了,默認是用 age 排序,但是有的時候需要用 id 來排序怎么辦? 這個時候比較器 :Comparator 就排上用場了。
Comparator 的使用有兩種方式:
- Collections.sort(list,Comparator<T>);
- list.sort(Comparator<T>);
其實主要是看 Comparator 接口的實現,重寫里面的 compare 方法。代碼如下:
//自定義排序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='關羽'} 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='關羽'} 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()排序的方法,希望對大家有所幫助。在此也非常感謝大家對腳本之家網站的支持!
相關文章
解決Eclipse add external jars運行出現java.lang.NoClassDefFoundErro
本篇文章對Eclipse add external jars導致運行出現java.lang.NoClassDefFoundError的解決方法進行了詳細的分析介紹。需要的朋友參考下2013-05-05Windows系統(tǒng)下JDK版本一鍵、自動切換工具圖文教程
這篇文章主要介紹了Windows系統(tǒng)下JDK版本一鍵、自動切換工具的相關資料,通過批處理腳本動態(tài)管理環(huán)境變量,支持JDK8-100版本,簡化多項目開發(fā),提升效率并減少配置錯誤,需要的朋友可以參考下2025-06-06淺談Java關閉線程池shutdown和shutdownNow的區(qū)別
本文主要介紹了Java關閉線程池shutdown和shutdownNow的區(qū)別,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09文件路徑正確,報java.io.FileNotFoundException異常的原因及解決辦法
這篇文章主要介紹了文件路徑正確,報java.io.FileNotFoundException異常的原因及解決辦法的相關資料,需要的朋友可以參考下2016-04-04Java編程實現軌跡壓縮之Douglas-Peucker算法詳細代碼
這篇文章主要介紹了Java編程實現軌跡壓縮之Douglas-Peucker算法詳細代碼,具有一定借鑒價值,需要的朋友可以參考。2017-11-11