1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
public class EmployeeTest {
@Test public void test2(){
TreeSet set = new TreeSet(new Comparator() { @Override public int compare(Object o1, Object o2) { if(o1 instanceof Employee && o2 instanceof Employee){ Employee e1 = (Employee)o1; Employee e2 = (Employee)o2;
MyDate b1 = e1.getBirthday(); MyDate b2 = e2.getBirthday();
return b1.compareTo(b2);
}
throw new RuntimeException("传入的数据类型不一致!"); } });
Employee e1 = new Employee("liudehua",55,new MyDate(1965,5,4)); Employee e2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4)); Employee e3 = new Employee("guofucheng",44,new MyDate(1987,5,9)); Employee e4 = new Employee("liming",51,new MyDate(1954,8,12)); Employee e5 = new Employee("liangzhaowei",21,new MyDate(1978,12,4));
set.add(e1); set.add(e2); set.add(e3); set.add(e4); set.add(e5);
Iterator iterator = set.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } }
@Test public void test1(){ TreeSet set = new TreeSet();
Employee e1 = new Employee("liudehua",55,new MyDate(1965,5,4)); Employee e2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4)); Employee e3 = new Employee("guofucheng",44,new MyDate(1987,5,9)); Employee e4 = new Employee("liming",51,new MyDate(1954,8,12)); Employee e5 = new Employee("liangzhaowei",21,new MyDate(1978,12,4));
set.add(e1); set.add(e2); set.add(e3); set.add(e4); set.add(e5);
Iterator iterator = set.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }
|