(集合四)Comparable介面示例
Comparable介面:
如果想要對某個集合元素之間實現比較,非自定義類可以通過工具類Collections中的sort()方法來實現自然排序(從小到大),自定義類必須通過實現Comparable<>介面且必須重寫方法int compareTo(T t)自定義排序規則來實現排序,之後再呼叫Collections中的sort方法(sort()方法底層是元素呼叫compareTo(E e)方法與另外一個元素比較)來對幾何元素實現排序。
升序規則(參照例二解釋):
當前物件大於引數物件,返回大於0的數;當前物件小於引數物件,返回小於10的數;當前物件等於引數物件,返回0;return this-o
降序規則(參照例二解釋):
引數物件大於當前物件,返回大於0的數;引數物件小於當前物件,返回小於0的數;當前物件等於引數物件,返回0;return o-this
例一:
package com.hyxy.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestComparable {
public static void main(String[] args) {
List<Character> l=new ArrayList<Character>();
for(int i=0;i<10;i++) {
int num=(int)(Math.random()*26);
l.add((char)(num+65));
}
System.out.println(l);//輸出結果:[K, S, F, E, Q, N, P, W, S, J]
Collections.sort(l);
System.out.println(l);//輸出結果:[E, F, J, K, N, P, Q, S, S, W]
}
}
例二:
//一自定義類Point
package com.hyxy.collection;
public class Point implements Comparable<Point>{
private int x;
private int y;
public Point() {}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String toString() {
return "("+x+","+y+")";
}
public int compareTo(Point o) {
return this.y-o.y; //按縱座標升序排序
}
//測試Point類
package com.hyxy.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestPoint {
public static void main(String[] args) {
Point p0=new Point(1,3);
Point p1=new Point(1,1);
Point p2=new Point(1,4);
Point p3=new Point(1,0);
Point p4=new Point(1,2);
List<Point> list=new ArrayList<Point>();
list.add(p0);
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
System.out.println(list);
//輸出結果:[(1,3), (1,1), (1,4), (1,0), (1,2)]
Collections.sort(list);
System.out.println(list);
//輸出結果:[(1,0), (1,1), (1,2), (1,3), (1,4)]
}
}
若在非自定義類(自定義類也可)中想要實現除了在自定義類中已經定義的CompareTo方法自定義排序,我們可以在使用該比較的類中寫一個內部類其中過載一個sort方法,sort(List list,Comparator c),當呼叫此方法時,在執行時系統呼叫的比較方法將不再是自定義類中的ComepareTo方法,而是呼叫Comparator中的compare(E e,E e)方法去進行比較。
例三(Point類為例二中自定義Point類):
package com.hyxy.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestPoint {
public static void main(String[] args) {
Point p0=new Point(1,3);
Point p1=new Point(1,1);
Point p2=new Point(1,4);
Point p3=new Point(1,0);
Point p4=new Point(1,2);
List<Point> list=new ArrayList<Point>();
list.add(p0);
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
System.out.println(list);//輸出結果:[(1,3), (1,1), (1,4), (1,0), (1,2)]
Collections.sort(list);
MyComparator c=new TestPoint().new MyComparator();
Collections.sort(list,c);
System.out.println(list);//輸出結果:[(1,0), (1,1), (1,2), (1,3), (1,4)]
}
public class MyComparator implements Comparator<Point>{
public int compare(Point o1, Point o2) {
return o1.getY()-o2.getY();
}
}
}
可以將例三中的MyComparator類更簡化一點,可以寫成匿名內部類,可以使程式碼看起來更加簡潔。
package com.hyxy.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestPoint {
public static void main(String[] args) {
Point p0=new Point(1,3);
Point p1=new Point(1,1);
Point p2=new Point(1,4);
Point p3=new Point(1,0);
Point p4=new Point(1,2);
Point[] po=new Point[5];
List<Point> list=new ArrayList<Point>();
for(int i=0;i<5;i++) {
list.add(po[i]);
}
System.out.println(list);//[(1,3), (1,1), (1,4), (1,0), (1,2)]
Collections.sort(list,new Comparator<Point>(){
public int compare(Point o1, Point o2) {
return o1.getY()-o2.getY();
}
});
System.out.println(list);//[(1,0), (1,1), (1,2), (1,3), (1,4)]
}
}
練習:要求定義一個Circle類,實現按照圓半徑從小到大排序,在測試類中,要求可以按照圓心距原點距離從小到大排序
自定義Circle類:
package com.hyxy.collection;
public class Circle implements Comparable<Circle>{
private double r;
private int x;
private int y;
public Circle() {}
public Circle(double r, int x, int y) {
this.r = r;
this.x = x;
this.y = y;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String toString() {
return "["+r+","+"("+x+","+y+")"+"]";
}
public int compareTo(Circle o) {
return (int)(this.r-o.r);
}
}
測試類:
package com.hyxy.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestCircle {
public static void main(String[] args) {
Circle c1=new Circle(3,2,2);
Circle c2=new Circle(6,1,1);
Circle c3=new Circle(2,3,3);
List<Circle> li=new ArrayList<Circle>();
li.add(c1);
li.add(c2);
li.add(c3);
System.out.println(li);//輸出結果:[[3.0,(2,2)], [6.0,(1,1)], [2.0,(3,3)]]
Collections.sort(li);
System.out.println(li);//輸出結果:[[2.0,(3,3)], [3.0,(2,2)], [6.0,(1,1)]]
Circle c4=new Circle(5,2,1);
li.add(c4);
System.out.println(li);//輸出結果:[[2.0,(3,3)], [3.0,(2,2)], [6.0,(1,1)], [5.0,(2,1)]]
Collections.sort(li, new Comparator<Circle>() {
public int compare(Circle o1, Circle o2) {
return (int)Math.sqrt((o1.getX()*o1.getX()+o1.getY()*o1.getY()))-
(int)Math.sqrt((o2.getX()*o2.getX()+o2.getY()*o2.getY()));
}
});
System.out.println(li);//輸出結果:[[6.0,(1,1)], [3.0,(2,2)], [5.0,(2,1)], [2.0,(3,3)]]
}
}