java集合(TreeSet 定制排序)

package Lian_Xi_Ji_He;


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


}






package Lian_Xi_Ji_He;


public class Employee1 {
private String name;
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;
}




@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;
Employee1 other = (Employee1) 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 Employee1(String name, int age, MyDate birthday) {
super();
this.name = name;
this.age = age;
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee1 [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";
}



}







package Lian_Xi_Ji_He;


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


public class Test1 {


public static void main(String[] args) {
Comparator com=new Comparator() {                                                        //创建一个实现Comparator接口的匿名类对象


@Override
public int compare(Object o1, Object o2) {                                                   //重写compare方法                                             


if(o1 instanceof Employee1&&o2 instanceof Employee1){
Employee1 e1=(Employee1)o1;
Employee1 e2=(Employee1)o2;
if(e1.getBirthday().getYear()!=e2.getBirthday().getYear()){                          //指明按哪个属性排序
return e1.getBirthday().getYear()-e2.getBirthday().getYear();
}else{
if(e1.getBirthday().getMonth()!=e2.getBirthday().getMonth()){
return e1.getBirthday().getMonth()-e2.getBirthday().getMonth();
}else{
return e1.getBirthday().getDay()-e2.getBirthday().getDay();
}
}
}
return 0;
}

};

TreeSet t=new TreeSet(com);                                                                 //将此对象作为形参加入到TreeSet构造函数中

Employee1 e1=new Employee1("mgy", 24, new MyDate(1992,7,30));
Employee1 e2=new Employee1("wf", 23, new MyDate(1993,11,21));                                 //此处是按出生年月日的先后排序
Employee1 e3=new Employee1("gj", 23, new MyDate(1993,1,30));
Employee1 e4=new Employee1("tghlx", 22, new MyDate(1993,1,10));
Employee1 e5=new Employee1("mgy1", 25, new MyDate(1991,7,30));

t.add(e1);
t.add(e2);
t.add(e3);
t.add(e4);
t.add(e5);
for(Object o:t){
System.out.println( o);
}
System.out.println(t.size());

}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值