Tree的使用练习

在这里插入图片描述

import java.util.TreeSet;

public class TestEmployee {
	public static void main(String[] args) {
		Employee e1 = new Employee("卢本伟",22,new MyDate(2,12,1994));
		Employee e2 = new Employee("柚柚妹",23,new MyDate(2,12,1993));
		Employee e3 = new Employee("五五开",24,new MyDate(2,12,1992));
		Employee e4 = new Employee("White",25,new MyDate(2,12,1995));
		Employee e5 = new Employee("丑丑开",62,new MyDate(2,12,1996));
		TreeSet set = new TreeSet();
		set.add(e1);
		set.add(e2);
		set.add(e3);
		set.add(e4);
		set.add(e5);
		System.out.println(set);
		for(Object str:set) {
			System.out.println(str);
		}
	}

}

public class Employee implements Comparable{
		private String name;
		private int age;
		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;
		}
		@Override
		public String toString() {
			return "Employee [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";
		}
		@Override
		public int compareTo(Object o) {
			if(o instanceof Employee) {
				Employee e = (Employee)o;
				return this.name.compareTo(e.name);
			}
			return 0;
		}
		
		public Employee(String name, int age, MyDate birthday) {
			super();
			this.name = name;
			this.age = age;
			this.birthday = birthday;
		}
		@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;
		}
		
		
}

public class MyDate {
	private int month;
	private int day;
	private int year;
	
	public MyDate(int month, int day, int year) {
		super();
		this.month = month;
		this.day = day;
		this.year = year;
	}
	@Override
	public String toString() {
		return "MyDate [month=" + month + ", day=" + day + ", year=" + year + "]";
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = 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 class TestEmployee1 {
	public static void main(String[] args) {
		Comparator com = new Comparator(){

			@Override
			public int compare(Object o1, Object o2) {
				if(o1 instanceof Employee && o2 instanceof Employee) {
					Employee d1 = (Employee)o1;
					Employee d2 = (Employee)o2;
					if(d1.getBirthday() == d2.getBirthday()) {
						return 0;
					}else if(d1.getBirthday().getYear() > d2.getBirthday().getYear()) {
						return 1;
					}else if(d1.getBirthday().getYear() < d2.getBirthday().getYear()) {
						return -1;
					}else {
						if(d1.getBirthday().getMonth() > d2.getBirthday().getMonth()) {
							return 1;
						}else if(d1.getBirthday().getMonth() < d2.getBirthday().getMonth()) {
							return -1;
						}else {
							return d1.getBirthday().getDay() - d2.getBirthday().getDay();
						}
						
					}
				}
				return 0;
			}
			
			
			
			
		};
		Employee e1 = new Employee("卢本伟",22,new MyDate(2,12,1994));
		Employee e2 = new Employee("柚柚妹",23,new MyDate(2,12,1993));
		Employee e3 = new Employee("五五开",24,new MyDate(2,12,1992));
		Employee e4 = new Employee("White",25,new MyDate(2,12,1995));
		Employee e5 = new Employee("丑丑开",62,new MyDate(2,12,1996));
		Employee e6 = new Employee("徐徐徐",62,new MyDate(1,12,1993));
		TreeSet set = new TreeSet(com);
		set.add(e1);
		set.add(e2);
		set.add(e3);
		set.add(e4);
		set.add(e5);
		set.add(e6);
		System.out.println(set);
		for(Object str:set) {
			System.out.println(str);
		}
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值