Comparable 簡介
Comparable 是排序介面,
若一個類實作了Comparable介面,就意味著“該類支持排序”,此外,“實作Comparable介面的類的物件”可以用作“有序映射(如TreeMap)”中的鍵或“有序集合(TreeSet)”中的元素,而不需要指定比較器,介面中通過x.compareTo(y)來比較x和y的大小,若回傳負數,意味著x比y小;回傳零,意味著x等于y;回傳正數,意味著x大于y,
Comparator 簡介
Comparator 是比較器介面,我們若需要控制某個類的次序,而該類本身不支持排序(即沒有實作Comparable介面);那么,我們可以建立一個“該類的比較器”來進行排序,這個“比較器”只需要實作Comparator介面即可,也就是說,我們可以通過“實作Comparator類來新建一個比較器”,然后通過該比較器對類進行排序,
int compare(T o1, T o2)和上面的x.compareTo(y)類似,定義排序規則后回傳正數,零和負數分別代表大于,等于和小于,
兩者的聯系
Comparable相當于“內部比較器”,而Comparator相當于“外部比較器”,
代碼實作
package com.github.compare;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @ _ooOoo_
* o8888888o
* 88" . "88
* (| -_- |)
* O\ = /O
* ____/`---'\____
* .' \\| |// `.
* / \\||| : |||// \
* / _||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' | |
* \ .-\__ `-` ___/-. /
* ___`. .' /--.--\ `. . __
* ."" '< `.___\_<|>_/___.' >'"".
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `-. \_ __\ /__ _/ .-` / /
* ======`-.____`-.___\_____/___.-`____.-'======
* `=---='
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* 佛祖保佑 永無BUG
*@DESCRIPTION Comparable是排序介面;若一個類實作了Comparable介面,就意味著“該類支持排序”,
* Comparable相當于“內部比較器”
*@AUTHOR SongHongWei
*@PACKAGE_NAME com.github.compare
**/
public class ComparableAndCompartor
{
public static void main(String[] args)
{
List<House> houses = new ArrayList();
House h1 = new House(95.0, 12000);
House h2 = new House(110.0, 12160);
House h3 = new House(80.0, 16300);
House h4 = new House(150.3, 10690);
houses.add(h1);
houses.add(h2);
houses.add(h3);
houses.add(h4);
comparable(houses);
comparator(houses);
}
/**
*@DESCRIPTION House類實作類Comparable介面, 并重寫了compareTo方法, 所以執行Collections.sort方法時會去呼叫我們重寫的compareTo方法
*@AUTHOR SongHongWei
*@TIME 2018/12/14-16:46
*@CLASS_NAME ComparableAndCompartor
**/
private static void comparable(List houses)
{
System.out.printf("未排序前的順序,%s\n", houses);
Collections.sort(houses);
System.out.printf("按面積大小排序后的順序,%s\n", houses);
}
private static void comparator(List houses)
{
System.out.printf("未排序前的順序,%s\n", houses);
Collections.sort(houses, new ComparatorDetail());
System.out.printf("按單價大小排序后的順序,%s\n", houses);
}
/**
*@DESCRIPTION 實作Compatator介面, 并重寫compare方法, 根據單價倒序排序
*@AUTHOR SongHongWei
*@TIME 2018/12/14-16:49
*@CLASS_NAME ComparableAndCompartor
**/
static class ComparatorDetail implements Comparator<House>
{
@Override
public int compare(House o1, House o2)
{
if (o1.price < o2.price)
return 1;
else if (o1.price > o2.price)
return -1;
return 0;
}
}
}
package com.github.compare;
/**
* @ _ooOoo_
* o8888888o
* 88" . "88
* (| -_- |)
* O\ = /O
* ____/`---'\____
* .' \\| |// `.
* / \\||| : |||// \
* / _||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' | |
* \ .-\__ `-` ___/-. /
* ___`. .' /--.--\ `. . __
* ."" '< `.___\_<|>_/___.' >'"".
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `-. \_ __\ /__ _/ .-` / /
* ======`-.____`-.___\_____/___.-`____.-'======
* `=---='
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* 佛祖保佑 永無BUG
*@DESCRIPTION 一個房子物件, 有面積和單價兩個屬性
*@AUTHOR SongHongWei
*@PACKAGE_NAME com.github.compare
**/
public class House implements Comparable<House>
{
/*房子的面積*/
protected double proportion;
/*房子每平米的售價*/
protected double price;
public House(double proportion, double price)
{
this.proportion = proportion;
this.price = price;
}
/**
*@DESCRIPTION 重寫compareTo方法, 利用房子的面積來進行大小比較
*@AUTHOR SongHongWei
*@TIME 2018/12/14-16:18
*@CLASS_NAME House
**/
@Override
public int compareTo(House o)
{
/*當前物件的面積大,回傳正數*/
if (this.proportion > o.proportion)
return 1;
/*當前面積小,回傳負數*/
else if (this.proportion < o.proportion)
return -1;
/*相等回傳0*/
return 0;
}
@Override
public String toString()
{
return "面積為" + proportion + "\t價格為" + price;
}
}
附注
Collection與Collections的區別
Collection是集合類的上級介面,繼承與他有關的介面主要有List和Set
Collections是針對集合類的一個幫助類,他提供一系列靜態方法實作對各種集合的搜索、排序、執行緒安全等操作
public static void main(String args[]) {
//注意List是實作Collection介面的
List list = new ArrayList();
double array[] = { 112, 111, 23, 456, 231 };
for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
Collections.sort(list); //把list按從小到大排序
for (int i = 0; i < array.length; i++) {
System.out.println(list.get(i));
}
// 結果:23.0 111.0 112.0 231.0 456.0
}
Collections如何呼叫重寫的compareTo方法的
集合框架中,Collections工具類支持兩種排序方法:
Collections.sort(List<T> list);
Collections.sort(List<T> list, Comparator<? super T> c)
如果待排序的串列中是數字或者字符,可以直接使用Collections.sort(list);當需要排序的集合或陣列不是單純的數字型時,需要自己定義排序規則,實作一個Comparator比較器,
Collections呼叫Collections.sort(list)方法,方法傳遞一個List集合,這里要求,List泛型里面裝的元素必須實作Compareable介面此外,串列中的所有元素都必須是可相互比較的(也就是說,對于串列中的任何 e1 和 e2 元素,e1.compareTo(e2) 不得拋出 ClassCastException),
Java原始碼里是這樣寫的
All elements in the list must implement the {@link Comparable}interface.Furthermore, all elements in the list must be <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a {@code ClassCastException} for any elements
Collections.sort原始碼
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
}
}
由原始碼可以看出來,sort內部呼叫了Arrays.sort的方法,繼續向下看
Arrays.sort原始碼
public static void sort(Object[] a) {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a);
else
ComparableTimSort.sort(a);
}
原始碼里首先判斷是否采用傳統的排序方法,LegacyMergeSort.userRequested屬性默認為false,也就是說默認選中 ComparableTimSort.sort(a)方法(傳統歸并排序在1.5及之前是默認排序方法,1.5之后默認執行ComparableTimSort.sort()方法,除非程式中強制要求使用傳統歸并排序,陳述句如下:System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"))
繼續看 ComparableTimSort.sort(a)原始碼
ComparableTimSort.sort(a)原始碼
static void sort(Object[] a) {
sort(a, 0, a.length);
}
static void sort(Object[] a, int lo, int hi) {
rangeCheck(a.length, lo, hi);
int nRemaining = hi - lo;
if (nRemaining < 2)
return; // Arrays of size 0 and 1 are always sorted
// If array is small, do a "mini-TimSort" with no merges
if (nRemaining < MIN_MERGE) {
int initRunLen = countRunAndMakeAscending(a, lo, hi);
binarySort(a, lo, hi, lo + initRunLen);
return;
}
/**
* March over the array once, left to right, finding natural runs,
* extending short natural runs to minRun elements, and merging runs
* to maintain stack invariant.
*/
ComparableTimSort ts = new ComparableTimSort(a);
int minRun = minRunLength(nRemaining);
do {
// Identify next run
int runLen = countRunAndMakeAscending(a, lo, hi);
// If run is short, extend to min(minRun, nRemaining)
if (runLen < minRun) {
int force = nRemaining <= minRun ? nRemaining : minRun;
binarySort(a, lo, lo + force, lo + runLen);
runLen = force;
}
// Push run onto pending-run stack, and maybe merge
ts.pushRun(lo, runLen);
ts.mergeCollapse();
// Advance to find next run
lo += runLen;
nRemaining -= runLen;
} while (nRemaining != 0);
// Merge all remaining runs to complete sort
assert lo == hi;
ts.mergeForceCollapse();
assert ts.stackSize == 1;
}
nRemaining表示沒有排序的物件個數,方法執行前,如果這個數小于2,就不需要排序了,
如果2<= nRemaining <=32,即MIN_MERGE的初始值,表示需要排序的陣列是小陣列,可以使用mini-TimSort方法進行排序,否則需要使用歸并排序,
mini-TimSort排序方法:先找出陣列中從下標為0開始的第一個升序序列,或者找出降序序列后轉換為升序重新放入陣列,將這段升序陣列作為初始陣列,將之后的每一個元素通過二分法排序插入到初始陣列中,注意,這里就呼叫到了我們重寫的compareTo()方法了,
private static int countRunAndMakeAscending(Object[] a, int lo, int hi) {
assert lo < hi;
int runHi = lo + 1;
if (runHi == hi)
return 1;
// Find end of run, and reverse range if descending
if (((Comparable) a[runHi++]).compareTo(a[lo]) < 0) { // Descending
while (runHi < hi && ((Comparable) a[runHi]).compareTo(a[runHi - 1]) < 0)
runHi++;
reverseRange(a, lo, runHi);
} else { // Ascending
while (runHi < hi && ((Comparable) a[runHi]).compareTo(a[runHi - 1]) >= 0)
runHi++;
}
return runHi - lo;
}
來源:blog.csdn.net/u010859650**/article/details/85009595
近期熱文推薦:
1.1,000+ 道 Java面試題及答案整理(2021最新版)
2.勁爆!Java 協程要來了,,,
3.玩大了!Log4j 2.x 再爆雷,,,
4.Spring Boot 2.6 正式發布,一大波新特性,,
5.《Java開發手冊(嵩山版)》最新發布,速速下載!
覺得不錯,別忘了隨手點贊+轉發哦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/394913.html
標籤:Java
