Set接口的小练习

 1 package Collection;
 2 public class MyDate {
 3         private  int year;
 4         private  int mouth;
 5         private  int day;
 6 
 7     @Override
 8     public String toString() {
 9         return this.year+":"+this.mouth+":"+this.day;
10     }
11 
12     public MyDate(int year, int mouth, int day) {
13             this.year = year;
14             this.mouth = mouth;
15             this.day = day;
16         }
17 
18         public int getYear() {
19             return year;
20         }
21 
22         public void setYear(int year) {
23             this.year = year;
24         }
25 
26         public int getMouth() {
27             return mouth;
28         }
29 
30         public void setMouth(int mouth) {
31             this.mouth = mouth;
32         }
33 
34         public int getDay() {
35             return day;
36         }
37 
38         public void setDay(int day) {
39             this.day = day;
40         }
41 
42 }

MyDate设置一下生日的属性,Eployee类中设置相应的属性

 

  1 /**
  2  * 如果使用测试类,要求当前类只能有一个空参构造器,否则不可以使用测试,只能通过main方法进行编译
  3  *
  4  */
  5 
  6 
  7 import org.junit.Test;
  8 
  9 import java.util.Arrays;
 10 import java.util.Comparator;
 11 import java.util.TreeSet;
 12 
 13 public class Employee implements Comparable{
 14     private String name;
 15     private int age;
 16     private MyDate birthday;
 17     @Override
 18     public String toString() {
 19         return "姓名:"+this.name+"年龄:"+this.age+"生日:"+this.birthday;
 20     }
 21 
 22     public Employee(String name, int age, MyDate birthday) {
 23         this.name = name;
 24         this.age = age;
 25         this.birthday = birthday;
 26     }
 27 
 28     public String getName() {
 29         return name;
 30     }
 31 
 32     public void setName(String name) {
 33         this.name = name;
 34     }
 35 
 36     public int getAge() {
 37         return age;
 38     }
 39 
 40     public void setAge(int age) {
 41         this.age = age;
 42     }
 43 
 44     public MyDate getBirthday() {
 45         return birthday;
 46     }
 47 
 48     public void setBirthday(MyDate birthday) {
 49         this.birthday = birthday;
 50     }
 51 @Test
 52     public void test2() {
 53         Employee employee1=new Employee("hpp",23,new MyDate(1998,3,18));
 54         Employee employee2=new Employee("ni",3,new MyDate(1997,1,20));
 55         Employee employee3=new Employee("ns",43,new MyDate(1992,8,1));
 56         Employee employee4=new Employee("tang",18,new MyDate(2000,5,15));
 57         Employee employee5=new Employee("an",25,new MyDate(1999,4,25));
 58         TreeSet treeSet = new TreeSet();
 59         treeSet.add(employee1);
 60         treeSet.add(employee2);
 61         treeSet.add(employee3);
 62         treeSet.add(employee4);
 63         treeSet.add(employee5);
 64         for (Object obj:treeSet){
 65             System.out.println(obj);
 66         }
 67     }
 68     //按照name进行排序
 69     @Override
 70     public int compareTo(Object o) {
 71         if (o instanceof Employee){
 72             Employee employee=(Employee) o;
 73             return this.getName().compareTo(employee.getName());
 74         }
 75         throw new RuntimeException("类型不匹配");
 76     }
 77     //按照生日日期先后排序
 78 
 79     public static void main(String[] args) {
 80         Employee employee1 = new Employee("hpp", 23, new MyDate(1998, 3, 18));
 81         Employee employee2 = new Employee("ni", 3, new MyDate(1997, 1, 20));
 82         Employee employee3 = new Employee("ns", 43, new MyDate(1992, 8, 1));
 83         Employee employee4 = new Employee("tang", 18, new MyDate(2000, 5, 15));
 84         Employee employee5 = new Employee("an", 25, new MyDate(1999, 4, 25));
 85         TreeSet treeSet2 = new TreeSet(new Comparator() {//直接新建一个Comparator类型的匿名对象
 86             @Override
 87             public int compare(Object o1, Object o2) {
 88                 if (o1 instanceof Employee && o2 instanceof Employee) {
 89                     Employee employee1 = (Employee) o1;
 90                     Employee employee2 = (Employee) o2;
 91                     if (employee1.birthday.getMouth() == employee2.birthday.getMouth()) {
 92                         if (employee1.birthday.getDay() > employee2.birthday.getDay()) {
 93                             return 1;
 94                         } else if (employee1.birthday.getDay() < employee2.birthday.getDay()) {
 95                             return -1;
 96                         } else {
 97                             return 0;
 98                         }
 99                     } else if (employee1.birthday.getMouth() > employee2.birthday.getMouth()) {
100                         return 1;
101                     } else {
102                         return -1;
103                     }
104                 }
105                 throw new RuntimeException("类型错误");
106             }
107         });
108         treeSet2.add(employee1);
109         treeSet2.add(employee2);
110         treeSet2.add(employee3);
111         treeSet2.add(employee4);
112         treeSet2.add(employee5);
113         for (Object obj:treeSet2){
114             System.out.println(obj);
115         }
116     }
117 }

 

posted @ 2021-11-05 17:19  tiiiiii  阅读(47)  评论(0)    收藏  举报