Comparable與Comparator的比較與使用
一概述
1.Comparable與Comparator使用背景
數值型資料(byte int short long float double)天生可比較大小,可排序,String實作了Comparable介面也可以比較大小與排序,而自訂類別多種多樣,沒有一個共有的可以用作排序的指標,因此需要在自訂類別中手動建立對比的方法,出於這個目的,java提供了兩個介面Comparable與Comparator。
2.集合排序
Collections.sort()底層排序依靠的是Arrays.sort(),而Arrays.sort()排序時採用的是冒泡法。
二Comparable
需要對比大小的物件可以實作Comparable接口,實作其中的抽象方法,該抽象方法用來設定比較的方式。以下以範例進行說明:
1.實體類別
package com.javase.collections.comparable;public class Student implements Comparable<Student> {private String name;private int score;public Student() {super(); }public Student(String name, int score) {super();this.name = name;this.score = score; }public String getName() {return name; }public void setName(String name) {this.name = name; }public int getScore() {return score; }public void setScore(int score) {this.score = score; } @Overridepublic int compareTo(Student stu) {return this.score - stu.score;// 操作对象减去参数对象,升序排列,反之降序。 } }
在compareTo()方法中,以屬性score為排序指標,採用“this.score-stu.score”,最終結果以升序排列,反之降序。
2.測試類別
package com.javase.collections.comparable;import java.util.ArrayList;import java.util.Collections;import java.util.List;import org.junit.Test;public class ComparableTest { @Testpublic void testComparable() { List<Student> stus = new ArrayList<Student>(); Student zhangsan = new Student("zhangsan", 100); Student lisi = new Student("lisi", 90); Student wanger = new Student("wanger", 95); stus.add(zhangsan); stus.add(lisi); stus.add(wanger); System.out.println("排序前");for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } System.out.println("排序后"); Collections.sort(stus);for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } } }
#輸出:
##三Comparator
如果一個類別在建立時未實作Comparable接口,希望在不修改源碼的情況下對其對象進行排序,可以在呼叫排序方法時實作Comparator比較器接口,指定排序方法。以下以範例進行說明:
1.實體類別
package com.javase.collections.comparator;public class Student {private String name;private int score;public Student() {super(); }public Student(String name, int score) {super();this.name = name;this.score = score; }public String getName() {return name; }public void setName(String name) {this.name = name; }public int getScore() {return score; }public void setScore(int score) {this.score = score; } }
2.測試類別
package com.javase.collections.comparator;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import org.junit.Test;public class ComparatorTest { @Testpublic void test() { List<Student> stus = new ArrayList<Student>(); Student zhangsan = new Student("zhangsan", 100); Student lisi = new Student("lisi", 90); Student wanger = new Student("wanger", 95); stus.add(zhangsan); stus.add(lisi); stus.add(wanger); System.out.println("排序前");for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } System.out.println("-----------------------"); Collections.sort(stus, new Comparator<Student>() { @Overridepublic int compare(Student stu01, Student stu02) {// return stu01.getScore() - stu02.getScore();//升序return stu02.getScore() - stu01.getScore();// 降序 } }); System.out.println("排序后");for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } } }
在compare(Student stu01, Student stu02)方法中,以屬性score為排序指標,採用“stu01.score-stu02.score”,最終結果升序排列,反之降序。
輸出:
以上是Comparable與Comparator的比較與使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

在使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名以構建查詢條件,是一個常見的難題。本文將針...
