Comparable是一個内比較器、是一個排序接口,實作Comparable接口的類可以自己和自己比較大小且表示該實作類支援排序,具體大小比較依賴Comparable中compareTo()方法的實作,接口實作:
package com.luna.model.strategy;
public interface Comparable {
public int compareTo(Object o);
}
Comparator是一個外比較器,是一政策模式的典型實作,當想比較的兩個類沒有實作Comparable接口或者compareTo()方法不支援自己想要的比較方式時,就可以使用Comparator,Comparator接口實作:
package com.luna.model.strategy;
public interface Comparator {
int compare(Object o1,Object o2);
}
Comparable的具體實作類Cat:
package com.luna.model.strategy;
public class Cat implements Comparable{
private String name;
private int height;
private int weight;
private Comparator comparator = new CatWeightComparator(); //使用外比較器Comparator來實作Comparable接口的compareTo方法
public Comparator getComparator() {
return comparator;
}
public void setComparator(Comparator comparator) {
this.comparator = comparator;
}
public Cat() {
super();
}
public Cat(String name, int height, int weight) {
super();
this.name = name;
this.height = height;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Cat [name=" + name + ", height=" + height + ", weight=" + weight + "]";
}
@Override
public int compareTo(Object o) {
return comparator.compare(this, o);
}
}
Comparable的具體實作類Dog:
package com.luna.model.strategy;
public class Dog implements Comparable{
private String name;
private int height;
private int weight;
public Dog() {
super();
}
public Dog(String name, int height, int weight) {
super();
this.name = name;
this.height = height;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Cat [name=" + name + ", height=" + height + ", weight=" + weight + "]";
}
@Override
public int compareTo(Object o) { //自己實作Comparable接口的compareTo()方法
if(o instanceof Dog){
Dog d = (Dog)o;
if(this.getHeight()>d.getHeight()) return 1;
else if(this.getHeight()<d.getHeight()) return -1;
else return 0;
}else{
return -100;
}
}
}
Comparator的具體實作類CatWeightComparator:
package com.luna.model.strategy;
public class CatWeightComparator implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Cat c1 = (Cat)o1;
Cat c2 = (Cat)o2;
if(c1.getWeight()>c2.getWeight()) return -1;
else if(c1.getWeight()<c2.getWeight()) return 1;
return 0;
}
}
對象排序工具類DataSorter:
package com.luna.model.strategy;
public class DataSorter {
public static void sort(Object[] o){
/**
* 冒泡排序:從後往前排序
*/
for (int i = o.length; i>0; i--) {
for (int j = 0; j < i-1; j++) {
Comparable o1 = (Comparable)o[j];
Comparable o2 = (Comparable)o[j+1];
if(o1.compareTo(o2)==1){
swap(o, j, j+1);
}
}
}
/**
* 冒泡排序:從前往後排序
*/
for (int i = 0; i < o.length - 1; i++) {
for (int j = 0; j < o.length - 1 - i; j++) {
Comparable o1 = (Comparable)o[j];
Comparable o2 = (Comparable)o[j+1];
if(o1.compareTo(o2)==1){
swap(o, j, j+1);
}
}
}
}
private static void swap(Object[] a, int x, int y) {
Object temp = a[x];
a[x] = a[y];
a[y] = temp;
}
public static void sort(Cat[] a){
for (int i = a.length; i>0; i--) {
for (int j = 0; j < i-1; j++) {
if(a[j].getHeight()>a[j+1].getHeight()){
swap(a,j,j+1);
}
}
}
}
private static void swap(Cat[] a, int x, int y) {
Cat temp = a[x];
a[x] = a[y];
a[y] = temp;
}
/**
* 冒泡排序算法
* @param a
*/
public static void sort(int[] a){
for (int i = a.length; i>0; i--) {
for (int j = 0; j < i-1; j++) {
if(a[j]>a[j+1]){
swap(a,j,j+1);
}
}
}
}
private static void swap(int[] a, int x, int y) {
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
public static void print(int[] a){
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void print(Cat[] c) {
for (int i = 0; i < c.length; i++) {
System.out.print(c[i]+"");
}
System.out.println();
}
public static void print(Object[] c) {
for (int i = 0; i < c.length; i++) {
System.out.print(c[i]+"");
}
System.out.println();
}
}
排序測試類Test:
package com.luna.model.strategy;
public class Test {
public static void main(String[] args) {
int a[] = {9,5,3,7,1};
DataSorter.sort(a);
DataSorter.print(a);
Cat c[] = {new Cat("Red",5,5),new Cat("blue",3,3), new Cat("pink",1,1)};
DataSorter.sort(c);
DataSorter.print(c);
}
}
Comparable&Comparator進階使用,請進傳送門>>