How To Sort Objects With Java Interface Comparable and Comparator

Java 提供了Comparable 和Comparator 两个接口让程序员可以实现Java对象的排序。

首先总结下Comparable 与Comparator接口的区别:

java.lang.Comparable

java.util.Comparator

int objOne.compareTo(objTwo)

int compare (objOne, objTwo)

返回:

   负数:  如果 objOne < objTwo

   :      如果 objOne == objTwo

   正数:   如果  objOne > objTwo

 

 

Comparable

必须修改要比较的类的结构,令其实现Comparable 接口

编写一个独立的类,令其实现Comparator接口

一个类只能有一种排序

一个类可以有多重排序方式,只要多编写一个Comparator的实现类就好了。

JAVA API中经常用到,例如:String, 包装类(Integer, Long, Double…, Date, Calendar …

用于对第三方类的排序,因为第三方类一般都是无法修改的,更别说实现Comparable了,所以只能使用Comparator了。

 

Comparable 实现对象方式:

package com.citi.crc;

public class AudiCar implements Comparable<AudiCar> {
	
	private String series;
	private Double price;
	private Double speed;
	
	public String getSeries() {
		return series;
	}

	public void setSeries(String series) {
		this.series = series;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Double getSpeed() {
		return speed;
	}

	public void setSpeed(Double speed) {
		this.speed = speed;
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(AudiCar o) {
		return this.getPrice().compareTo(o.getPrice());
	}
}

package com.citi.crc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestAudiCar {

	public static void main(String[] args) {
		List<AudiCar> cars = new ArrayList<AudiCar>();
		
		AudiCar audiA3 = new AudiCar();
		audiA3.setSeries("A3");
		audiA3.setPrice(200000d);
		audiA3.setSpeed(80d);

		AudiCar audiA4 = new AudiCar();
		audiA4.setSeries("A4");
		audiA4.setPrice(260000d);
		audiA4.setSpeed(90d);
		
		AudiCar audiA6 = new AudiCar();
		audiA6.setSeries("A6");
		audiA6.setPrice(320000d);
		audiA6.setSpeed(100d);
		
		cars.add(audiA4);
		cars.add(audiA6);
		cars.add(audiA3);
		
		Collections.sort(cars);
//		Collections.reverse(cars);
		for (AudiCar car : cars) {
			System.out.println(car.getPrice());
		}
	}
}


 

Comparator 实现排序方式:

package com.citi.crc;

import java.util.Comparator;

public class AudiCar {
	
	private String series;
	private Double price;
	private Double speed;
	
	public String getSeries() {
		return series;
	}

	public void setSeries(String series) {
		this.series = series;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Double getSpeed() {
		return speed;
	}

	public void setSpeed(Double speed) {
		this.speed = speed;
	}

	static class PriceComparator implements Comparator<AudiCar> {

		/* (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		@Override
		public int compare(AudiCar o1, AudiCar o2) {
			return o1.getPrice().compareTo(o2.getPrice());
		}
		
	}

	static class SpeedComparator implements Comparator<AudiCar> {
		/* (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		@Override
		public int compare(AudiCar o1, AudiCar o2) {
			return o1.getSpeed().compareTo(o2.getSpeed());
		}
	}
}

package com.citi.crc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestAudiCar {

	public static void main(String[] args) {
		List<AudiCar> cars = new ArrayList<AudiCar>();
		
		AudiCar audiA3 = new AudiCar();
		audiA3.setSeries("A3");
		audiA3.setPrice(200000d);
		audiA3.setSpeed(80d);

		AudiCar audiA4 = new AudiCar();
		audiA4.setSeries("A4");
		audiA4.setPrice(260000d);
		audiA4.setSpeed(90d);
		
		AudiCar audiA6 = new AudiCar();
		audiA6.setSeries("A6");
		audiA6.setPrice(320000d);
		audiA6.setSpeed(100d);
		
		cars.add(audiA4);
		cars.add(audiA6);
		cars.add(audiA3);
		
		Collections.sort(cars, new AudiCar.PriceComparator());
//		Collections.sort(cars, new AudiCar.SpeedComparator());

		for (AudiCar car : cars) {
			System.out.println(car.getPrice());
		}
	}
}
阅读更多
个人分类: Java/J2EE
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭