Java的两种比较器用法:Comparable和Comparator

    Java有两种比较器:Comparable和Comparator。Comparable称之为内部比较器,实体类实现该接口,重写compareTo方法;

Comparator称之为外部比较器,可以自定义比较器类,实现该接口,重写compare方法。Comparator使用起来比Comparable更加的灵活,方便。以下是两种比较器具体的使用实例:

1、内部比较器Comparable示例

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

public class compareDemo {
	/*
	 * 内部比较器Comparable示例
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		List<Student> studentList = new ArrayList<Student>();
		Student s1 = new Student("tom", "男", 23);
		Student s2 = new Student("Jon", "男", 22);		
		Student s3 = new Student("Quy", "男", 20);
		Student s4 = new Student("Weg", "男", 21);
		Student s5 = new Student("Kiy", "女", 20);
		studentList.add(s1);
		studentList.add(s2);
		studentList.add(s3);
		studentList.add(s4);
		studentList.add(s5);
		System.out.println("排序前:"+studentList);
		Collections.sort(studentList);
		System.out.println("排序后:"+studentList);
	}
}

@SuppressWarnings({"rawtypes","unused"})
class Student implements Comparable {
	private String name;
	private String gender;
	private int age;

	public Student(String name, String gender, int age) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", gender=" + gender + ", age=" + age + "]";
	}

	/*
	 * (non-Javadoc)先按照年龄比较,再按照姓名比较
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(Object object) {
		int compareResult = 0;
		Student student = (Student)object;
		compareResult = age > student.age ? 1 : (age == student.age ? 0 : -1);
		if(compareResult == 0) {
			compareResult = name.charAt(0) > student.name.charAt(0) ? 1 : (name.charAt(0) == student.name.charAt(0) ? 0 : -1);
		}
		return compareResult;
	}
	
}

1、外部比较器Comparator示例

    Comparator有两种用法,一种是提前自定义比较器类,二种是使用内名内部类。具体示例如下:

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

public class compareDemo {
	/*
	 * 外部比较器Comparator示例
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void main(String[] args) {
		List<Student> studentList = new ArrayList<Student>();
		Student s1 = new Student("tom", "男", 23);
		Student s2 = new Student("Jon", "男", 22);		
		Student s3 = new Student("Quy", "男", 20);
		Student s4 = new Student("Weg", "女", 21);
		Student s5 = new Student("Kiy", "女", 20);
		studentList.add(s1);
		studentList.add(s2);
		studentList.add(s3);
		studentList.add(s4);
		studentList.add(s5);
		//1、使用方法1:自定义外部比较器类
		System.out.println("使用方法1排序前:"+studentList);
		Collections.sort(studentList,new StudentCompare());
		System.out.println("使用方法1排序后:"+studentList);
		studentList.clear();
		studentList.add(s1);
		studentList.add(s2);
		studentList.add(s3);
		studentList.add(s4);
		studentList.add(s5);
		//2、使用方法2:采用匿名内部类
		System.out.println("使用方法2排序前:"+studentList);
		Collections.sort(studentList,new Comparator() {
			@Override
			public int compare(Object object1, Object object2) {
				int compareResult = 0;
				Student stu1 = (Student)object1;
				Student stu2 = (Student)object2;
				compareResult = stu1.gender.charAt(0) > stu2.gender.charAt(0) ? 1 : (stu1.gender.charAt(0) == stu2.gender.charAt(0) ? 0 : -1);
				if(compareResult == 0) {		
					compareResult = stu1.age > stu2.age ? 1 : (stu1.age == stu2.age ? 0 : -1);
				}
				if(compareResult == 0) {
					compareResult = stu1.name.charAt(0) > stu2.name.charAt(0) ? 1 : (stu1.name.charAt(0) == stu2.name.charAt(0) ? 0 : -1);
				}
				return compareResult;
			}
		});	
		System.out.println("使用方法2排序后:"+studentList);
	}
}

/**
 * 外部比较器Comparator:先按照性别比较,再按照年龄比较,再按照姓名比较
 */
@SuppressWarnings("rawtypes")
class StudentCompare implements Comparator {

	@Override
	public int compare(Object object1, Object object2) {
		int compareResult = 0;
		Student stu1 = (Student)object1;
		Student stu2 = (Student)object2;
		compareResult = stu1.gender.charAt(0) > stu2.gender.charAt(0) ? 1 : (stu1.gender.charAt(0) == stu2.gender.charAt(0) ? 0 : -1);
		if(compareResult == 0) {		
			compareResult = stu1.age > stu2.age ? 1 : (stu1.age == stu2.age ? 0 : -1);
		}
		if(compareResult == 0) {
			compareResult = stu1.name.charAt(0) > stu2.name.charAt(0) ? 1 : (stu1.name.charAt(0) == stu2.name.charAt(0) ? 0 : -1);
		}
		return compareResult;
	}	
}

@SuppressWarnings({"rawtypes","unused"})
class Student implements Comparable {
	public String name;
	public String gender;
	public int age;

	public Student(String name, String gender, int age) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", gender=" + gender + ", age=" + age + "]";
	}

	/*
	 * (non-Javadoc)先按照年龄比较,再按照姓名比较
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(Object object) {
		int compareResult = 0;
		Student student = (Student)object;
		compareResult = age > student.age ? 1 : (age == student.age ? 0 : -1);
		if(compareResult == 0) {
			compareResult = name.charAt(0) > student.name.charAt(0) ? 1 : (name.charAt(0) == student.name.charAt(0) ? 0 : -1);
		}
		return compareResult;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值