定义一个Employee类
该类包括:
定义Mydate类包含:
创建该Employee类的5个对象,并把这些对象放入TreeSet集合中,分别按以下两种方式对集合中的元素进行排序,并遍历输出
(1)使Employee实现Comparable接口,并按name排序,字典表顺序
(2)创建TreeSet时传入Comparator对象,按生日日期的对员工进行先后顺序排序
1992 - 05 - 12
1992 - 05 - 18
1992 - 07 - 15
1993 - 06 - 15
1998 - 02 - 03
public class Employee implements Comparable{
private String name;;
private int age;
private MyDate birthday;
public Employee(){}
public Employee(int age){
this.age = age;
}
public Employee(String name){this.name = name;}
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = 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 compareTo(Employee e) {
if(this.getAge() == e.getAge()) {
return this.getName().compareTo(e.getName());
}else{
return this.getAge() - e.getAge();
}
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + ''' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(){
}
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = 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 int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
}
public class Test {
public static void main(String[] args) {
//创建TreeSet集合对象
TreeSet set = new TreeSet<>(new Comparator() {//比较器比较生日
@Override
public int compare(Employee o1, Employee o2) {
int year = o1.getBirthday().getYear();
int month = o1.getBirthday().getMonth();
int day = o1.getBirthday().getDay();
int year1 = o2.getBirthday().getYear();
int month1 = o2.getBirthday().getMonth();
int day1 = o2.getBirthday().getDay();
int result = year - year1;
int result2 = result == 0 ? month - month1 : result ;
int result3 = result2 ==0 ? day - day1 : result2;
return result3;
}
});
//创建Empployee对象
Employee e1 = new Employee("zhangsan",20,new MyDate(1992,05,12));
Employee e2 = new Employee("lisi",18,new MyDate(1992,05,18));
Employee e3 = new Employee("wangwu",18,new MyDate(1992,07,15));
Employee e4 = new Employee("zhaoliu",13,new MyDate(1992,05,12));
Employee e5 = new Employee("xuqi",80,new MyDate(1993,06,15));
//把Employee对象放到TreeSet集合中
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
//遍历
for(Employee e : set){
System.out.println(e);
}
}
}
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
小编已加密:aHR0cHM6Ly9kb2NzLnFxLmNvbS9kb2MvRFVrVm9aSGxQZUVsTlkwUnc==出于安全原因,我们把网站通过base64编码了,大家可以通过base64解码把网址获取下来。