java TreeSet自然排序和定制排序

import java.lang.Comparable;
import java.util.Comparator;
import java.util.TreeSet;

//TreeSet自然排序与定制排序,

//1. 自然排序需要类实现Comparable接口,重写compareTo(),由于Set的不可重复性,要保证具有相同属性的元素不被重复添加,就需 要重写添加进Set的元素类的equals()和hashCode(),以及toString()方便遍历打印,如果不重写toString(),打印的是哈希值。

//2. 定制排序不需要,只需要某个匿名类实现Comparator接口,将该类对象传给TreeSet构造器即可

class TestTreeSet{

    public static void main(String[] args){

//********************定制排序**********//

        Comparator cp = new Comparator(){
        public int compare(Object obj1,Object obj2){
        if(obj1 instanceof Employee && obj2 instanceof Employee){
                Employee o1 = (Employee) obj1;
                Employee o2 = (Employee) obj2;
                int year = o1.getBirthday().getYear() - o2.getBirthday().getYear();
                if(year == 0){
                   int month = o1.getBirthday().getMonth()-(o2.getBirthday().getMonth());
                   if(month == 0){
                       int day = o1.getBirthday().getDay()-(o2.getBirthday().getDay());
                        return day;
                   }else{
                    return month;
                   }
                }else{
                return year;
                }
        }
        return 0;
        }
       };

       TreeSet ts = new TreeSet(cp);
       ts.add(new Employee("xiaoming",12,new MyDate(5,18,1999)));
       ts.add(new Employee("minghua",12,new MyDate(2,18,1989)));
       ts.add(new Employee("liumign",12,new MyDate(2,18,1997)));
       ts.add(new Employee("liumign",12,new MyDate(2,18,1997)));
       ts.add(new Employee("liumign",12,new MyDate(2,18,1997)));
       System.out.println(ts);
    }
}
class Employee {
//implements Comparable{
private String name;
private int age;
private MyDate birthday;
    public Employee(String name,int age ,MyDate birth){
    this.name = name;
    this.age = age;
    this.birthday = birth;
    }
   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 birth){
        this.birthday = birth;
     }
    public String toString(){
          return this.name +" " +this.age+" " + this.birthday;

    }

//*******************自然排序**************/

    /*public int compareTo(Object obj){
       if(obj instanceof Employee){
         Employee e = (Employee) obj;
          return this.name.compareTo(e.name);         
        }
        return 0;
    }*/

    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;
    }
   
    public boolean equals(Object obj){
      if(this == obj){
           return true;
      }
      if(obj == null){
           return false;
      }
      if(getClass() != obj.getClass()){
            return false;
      }
      Employee e =(Employee) obj;
      if(this.age != e.age){
         return false;
      }
      if(this.name == null){
      if(e.name !=null){
           return false;
       }
      }else if(! this.name.equals(e.name)){
           return false;
      }
    if(this.birthday == null){
      if(e.birthday !=null){
           return false;
       }
      }else if(! this.birthday.equals(e.birthday)){
           return false;
      }
      return true;
    }
}
class MyDate{
private int month;
private int day;
private int year;
    public MyDate(int m,int d,int y){
       month  =m;
       day = d;
       year =y;
    }
public void setMonth(int month){
this.month = month;
}
public int getMonth(){
return month;
}
    public int getDay(){
    return day;
    }
    public void setDay(int day){
    this.day = day;
    }
    public void setYear(int year){
    this.year = year;
    }
    public int getYear(){
    return year;
    }
    public String toString(){
   
           return year + "."+ month +"."+day;          
    }
    public int hashCode(){
    final int prime = 31;
    int result = 1;
    result = prime * result + day;
    result = prime * result + month;
    result = prime * result + year;
    return result;
    }
   public boolean equals(Object obj){
      if(this == obj){
           return true;
      }
      if(obj == null){
           return false;
      }
      if(getClass() != obj.getClass()){
            return false;
      }
      MyDate d = (MyDate) obj;
      if(day != d.day){
          return false;
      }
      if(month != d.month){
          return false;
      }
      if(year != d.year){
          return false;
      }
      return true;
     }
}

猜你喜欢

转载自blog.csdn.net/sue12347/article/details/80267866