1. 程式人生 > >TreeSet的排序兩種實現方式Comparator和Comparable

TreeSet的排序兩種實現方式Comparator和Comparable

TreeSet

條件

型別一樣

設計到排序

comparable是自然(可以修改類的情況下)

comparator是定製(不可以修改類的情況下)優先順序更高

 使用說明:

一般採用的是自然排序,但是當,不能對類進行修改時不得不採用comparator方法,下面的demo採用了兩種方式結合。

程式碼整體說明:

Employee類的birthday屬性是自定義類MyDate型別

Employee採用comparable方式預設的是按著姓名進行排序

然後採用定製的Comparator方式對出生日期進行排序

package TestContainer;

public class MyDate {
	private int day;
	private int month;
	private int year;

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getYear() {
		return year;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + day;
		result = prime * result + month;
		result = prime * result + year;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MyDate other = (MyDate) obj;
		if (day != other.day)
			return false;
		if (month != other.month)
			return false;
		if (year != other.year)
			return false;
		return true;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public MyDate(int day, int month, int year) {
		super();
		this.day = day;
		this.month = month;
		this.year = year;
	}

	public MyDate() {
		super();
	}

	@Override
	public String toString() {
		return "MyDate [day=" + day + ", month=" + month + ", year=" + year + "]";
	}

}

package TestContainer;
//是否重寫compareTo()和equals()方法
//雖然比較的是String或者Integer的compareTo()方法,但是,把Employee放到TreeSet中還需要判斷多個Employee是否相等
public class Employee implements Comparable{
	private String name;
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee other = (Employee) obj;
		if (age != other.age)
			return false;
		if (birthday == null) {
			if (other.birthday != null)
				return false;
		} else if (!birthday.equals(other.birthday))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	private int age;
	private MyDate birthday;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public MyDate getBirthday() {
		return birthday;
	}

	public void setBirthday(MyDate birthday) {
		this.birthday = birthday;
	}

	public Employee() {
		super();
	}

	public Employee(String name, int age, MyDate birthday) {
		super();
		this.name = name;
		this.age = age;
		this.birthday = birthday;
	}

	@Override
	public String toString() {
		return "Employee [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";
	}

	@Override
	public int compareTo(Object o) {
		if(o instanceof Employee){
			Employee employee=(Employee)o;
			int i=this.getName().compareTo(employee.getName());
//			if(i==0){
//				int j=this.getAge()-employee.getAge();
////				if (j==0){
////					
////				}
//				return j;
//			}
			return i;
		}
		return 0;
	}
}

package TestContainer;

import static org.junit.Assert.*;

import java.util.Comparator;
import java.util.TreeSet;

import org.junit.Test;

public class TestEmployee {

	// 自然排序,Employee實現Comparable介面,並按著Name排序
	@Test
	public void test() {
		Employee e1 = new Employee("Haid", 31, new MyDate(2, 2, 1985));
		Employee e2 = new Employee("Qiao", 25, new MyDate(13, 10, 1991));
		Employee e3 = new Employee("Alex", 20, new MyDate(2, 2, 1996));
		Employee e4 = new Employee("Tom", 37, new MyDate(2, 2, 1980));
		Employee e5 = new Employee("Alex", 28, new MyDate(13, 10, 1989));

		// 定製排序Comparator
		Comparator comparator = new Comparator() {
			public int compare(Object o1, Object o2) {
				if (o1 instanceof Employee && o2 instanceof Employee) {
					Employee e1 = (Employee) o1;
					Employee e2 = (Employee) o2;
					int i = e1.getBirthday().getYear() - e2.getBirthday().getYear();
					if (i == 0) {
						int j = e1.getBirthday().getMonth() - e2.getBirthday().getMonth();
						if (j == 0) {
							return e1.getBirthday().getDay() - e2.getBirthday().getDay();
						}
						return j;
					}
					return i;
				}
				return 0;

			}

		};

		TreeSet set = new TreeSet(comparator);
		set.add(e1);
		set.add(e2);
		set.add(e3);
		set.add(e4);
		set.add(e5);

		for (Object object : set)
			System.out.println(object);

	}
}