Java对象排序Comparable和Comparator

2022-08-11 08:35:12

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高级使用,请进传送门>>

  • 作者:抽离的心
  • 原文链接:https://blog.csdn.net/u011635492/article/details/80157678
    更新时间:2022-08-11 08:35:12