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);

	}
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值