Comparator接口

Comparator 接口可以实现任意多种方式的排序选择;
Comparator 接口可以不修改需要排序的类的源代码


Comparator 接口的实现类必须实现一个
int compare(Object o1, Object o2)方法

此方法对 o1 和 o2 对象进行比较

package cn.veryedu.zwx.arrays.comparator;

public class Emp {

	private int empNo;
	private String name;
	private double salary;
	private int age;
	
	public int getEmpNo() {
		return empNo;
	}
	public void setEmpNo(int empNo) {
		this.empNo = empNo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + empNo;
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Emp other = (Emp) obj;
		if (empNo != other.empNo)
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return "(empNo=" + empNo + ", name=" + name
				+ ", age=" + age + ", salary=" + salary + ")";
	}
}

package cn.veryedu.zwx.arrays.comparator;

import java.util.Arrays;

import cn.veryedu.zwx.arrays.comparator.impl.AgeComparator;
import cn.veryedu.zwx.arrays.comparator.impl.EmpNoComparator;
import cn.veryedu.zwx.arrays.comparator.impl.NameComparator;
import cn.veryedu.zwx.arrays.comparator.impl.SalComparator;

public class Test {

	public static void main(String[] args) {
		Emp[] emps = new Emp[5];
		
		Emp e0 = new Emp();
		e0.setEmpNo(1008);
		e0.setName("zhang");
		e0.setSalary(3000);
		e0.setAge(21);
		emps[0] = e0;
		
		Emp e1 = new Emp();
		e1.setEmpNo(1002);
		e1.setName("wang");
		e1.setSalary(5000);
		e1.setAge(23);
		emps[1] = e1;
		
		Emp e2 = new Emp();
		e2.setEmpNo(1009);
		e2.setName("liu");
		e2.setSalary(4000);
		e2.setAge(20);
		emps[2] = e2;
		
		Emp e3 = new Emp();
		e3.setEmpNo(1014);
		e3.setName("li");
		e3.setSalary(3600);
		e3.setAge(27);
		emps[3] = e3;
		
		Emp e4 = new Emp();
		e4.setEmpNo(1100);
		e4.setName("chen");
		e4.setSalary(6000);
		e4.setAge(29);
		emps[4] = e4;
		
		System.out.println(Arrays.toString(emps));
		System.out.println();
		
		//选择对Emp数组中的对象通过 empNo 进行排序
		Arrays.sort(emps, new EmpNoComparator());
		
		System.out.println("选择对Emp数组中的对象通过 empNo 进行排序------------------------");
		System.out.println(Arrays.toString(emps));
		System.out.println();
		
		//选择对Emp数组中的对象通过 name 进行排序
		Arrays.sort(emps, new NameComparator());
		
		System.out.println("选择对Emp数组中的对象通过 name 进行排序------------------------");
		System.out.println(Arrays.toString(emps));
		System.out.println();
		
		//选择对Emp数组中的对象通过 age 进行排序
		Arrays.sort(emps, new AgeComparator());
		
		System.out.println("选择对Emp数组中的对象通过 age 进行排序------------------------");
		System.out.println(Arrays.toString(emps));
		System.out.println();
		
		//选择对Emp数组中的对象通过 salary 进行排序
		Arrays.sort(emps, new SalComparator());
		
		System.out.println("选择对Emp数组中的对象通过 salary 进行排序------------------------");
		System.out.println(Arrays.toString(emps));
		System.out.println();
	}

}

package cn.veryedu.zwx.arrays.comparator.impl;

import java.util.Comparator;

import cn.veryedu.bank.BankAccount;

public class AccountNoComparator implements Comparator<BankAccount> {

	@Override
	public int compare(BankAccount a1, BankAccount a2) {
		return a1.getAccountNo() - a2.getAccountNo();
	}

}

package cn.veryedu.zwx.arrays.comparator.impl;

import java.util.Comparator;

import cn.veryedu.zwx.arrays.comparator.Emp;

public class SalComparator implements Comparator<Emp> {

	@Override
	public int compare(Emp e1, Emp e2) {
		if (e1.getSalary() == e2.getSalary()) {
			return 0;
		} else if (e1.getSalary() > e2.getSalary()) {
			return 1;
		} else {
			return -1;
		}
	}
}

package cn.veryedu.zwx.arrays.comparator.impl;

import java.util.Comparator;

import cn.veryedu.zwx.arrays.comparator.Emp;

public class NameComparator implements Comparator<Emp> {

	@Override
	public int compare(Emp e1, Emp e2) {
		return e1.getName().compareTo(e2.getName());
	}

}

package cn.veryedu.zwx.arrays.comparator.impl;

import java.util.Comparator;

import cn.veryedu.zwx.arrays.comparator.Emp;

public class AgeComparator implements Comparator<Emp> {

	@Override
	public int compare(Emp e1, Emp e2) {
		return e1.getAge() - e2.getAge();
	}

}

package cn.veryedu.zwx.arrays.comparator.impl;

import java.util.Comparator;

import cn.veryedu.zwx.arrays.comparator.Emp;

public class EmpNoComparator implements Comparator<Emp> {

	@Override
	public int compare(Emp e1, Emp e2) {
//		if (e1.getEmpNo() == e2.getEmpNo()) {
//			return 0;
//		} else if (e1.getEmpNo() > e2.getEmpNo()) {
//			return 1;
//		} else {
//			return -1;
//		}
		
		return e1.getEmpNo() - e2.getEmpNo();
	}

}


你可能感兴趣的:(String,object,null,equals,Class,360)