1.What is Comparable interface
This interface forces the whole sort of objects for each class that implements it. This sort is calledNatural sorting, of typecompareTo The method is called itsNatural comparison method. The list of objects that implement this interface (andArray) can be passed (and ) perform automatic sorting. Objects that implement this interface can be used as keys in ordered map tables or elements in ordered sets without specifyingComparator. Highly recommended (although not required) to align natural sorting with equals. The so-called consistency with equals refers to the classC Every onee1ande2 For example, if and only if(((Object)e2) == 0) and((Object)e2) When having the same boolean value, the classC The natural sorting is calledConsistent with equals 。
2. What method to implement
int compareTo(T o) Compare the order of this object to the specified object。If the object is smaller than、Equal to or greater than the specified object,Return negative integers respectively、Zero or positive integer。
Highly recommended ((y)==0) == ((y)) This practice,But not Strictly require this。Generally speaking,Any implementation Comparable Both interfaces and classes that violate this condition should clearly point out this fact。Recommend this explanation:“Notice:Such a class has equals Inconsistent natural sorting。”
parameter: o - Objects to compare。 return:
Negative integers、Zero or positive integer,According to this object is less than、Equal to or greater than the specified object。 Throw out:
ClassCastException - If the specified object's type does not allow it to be compared with this object。
3. Example
package test1; public class Note<T> implements Comparable<Note<T>> { private T data; //data private int weight; //Weight private Note<T> left; //Left child private Note<T> right; //Right child public Note(T data,int weight){ this.data=data; this.weight=weight; } @Override public String toString(){ return "data="+this.data+",weitht="+this.weight; } public T getData() { return data; } public void setData(T data) { this.data = data; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public Note<T> getLeft() { return left; } public void setLeft(Note<T> left) { this.left = left; } public Note<T> getRight() { return right; } public void setRight(Note<T> right) { this.right = right; } /*** In reverse order.*/ @Override public int compareTo(Note<T> o) { if(>this.weight){ return 1; }else if(<this.weight){ return -1; } return 0; } /*** Ascending order*/ // @Override // public int compareTo(Note<T> o){ // if(>){ // return 1; // }else if(<){ // return -1; // } // return 0; // }
Ps: (Fast Memory Method) The current object is compared with the next object. If the comparison result is 1 for exchange, the others are not exchanged.
When the latter object is larger than the current object and the result value is 1, the exchange is before and after, indicating that it is arranged in reverse order.
When the latter object is smaller than the current object and the result value is 1, it is exchanged before and after, indicating that it is an ascending order.