java集合(TreeSet自然排序)

本文介绍了一个使用Java实现的日期类MyDate及员工类Employee,其中包含成员变量、构造方法、get和set方法等,并通过TreeSet集合实现了员工信息的排序与存储。通过具体的示例展示了如何创建员工对象并添加到TreeSet中,最后遍历输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 Employee implements Comparable{           //实现Comparable接口
private String name;                               //重写comparTo方法
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;
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 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 e=(Employee)o;
return this.getName().compareTo(e.getName());                                                      //在compareTo方法中指明按哪个属性进行排序,此处按姓名排序

}
return 0;
}



}







package Lian_Xi_Ji_He;


import java.util.Iterator;
import java.util.TreeSet;


public class Test {
public static void main(String[] args) {

Employee e1=new Employee("mgy", 24, new MyDate(1992,7,30));
Employee e2=new Employee("wf", 23, new MyDate(1993,11,21));
Employee e3=new Employee("gj", 23, new MyDate(1993,1,30));
Employee e4=new Employee("tghlx", 22, new MyDate(1993,1,10));
Employee e5=new Employee("mgy1", 25, new MyDate(1991,7,30));

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

Iterator i=t.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
System.out.println(t.size());

}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值