TreeSet的两种排序方式

示例:

示例公用类:

public class MyDate {
    private int month;

    private int year;
    private int day;
    @Override
    public String toString() {
        return "MyDate{" +
                "month=" + month +
                ", year=" + year +
                ", day=" + day +
                '}';
    }

    public MyDate(int month, int year, int day) {
        this.month = month;
        this.year = year;
        this.day = day;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }
}

自然排序:在需要排序的属性的类中重写compareTo()方法,完成TreeSet集合按Employee的name属性排序:

public class Employee implements Comparable {
    private String name;
    private int age;
    private MyDate birthday;

    public Employee(String name, int age, MyDate birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public Employee() {

    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", 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(Object o) {
        if(o instanceof Employee){
            Employee e = (Employee)o;
            return this.name.compareTo(e.name);
        }
        return 0;

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




        TreeSet tree = new TreeSet();

        Employee employee1 = new Employee("yangshijie",20,new MyDate(2001,7,23));
        Employee employee2 = new Employee("yanzhihang",18,new MyDate(2003,1,9));
        Employee employee3 = new Employee("chenkangjia",18,new MyDate(2003,1,9));
        Employee employee4 = new Employee("luoxingzhe",20,new MyDate(2002,7,23));
        Employee employee5 = new Employee("wangwei",20,new MyDate(2001,8,23));
        tree.add(employee1);
        tree.add(employee2);
        tree.add(employee3);
        tree.add(employee4);
        tree.add(employee5);
        Iterator iterator = tree.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }



    }

}

示例二:定制排序,创建comparator对象并并重写compare()方法,将创建的对象作为元素传入TreeSet的构造器中实现排序。 

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



        Comparator com = 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;
                    if (e1.getBirthday().getYear()!=e2.getBirthday().getYear()){
                        return e1.getBirthday().getYear()-e2.getBirthday().getYear();
                    }else if(e1.getBirthday().getMonth()!=e2.getBirthday().getMonth()){
                        return e1.getBirthday().getMonth()-e2.getBirthday().getMonth();
                    }else{
                        return e1.getBirthday().getDay()-e2.getBirthday().getDay();
                    }

                }
                throw new RuntimeException("传入数据错误!");
            }

        };

        TreeSet tree = new TreeSet(com);

        Employee employee1 = new Employee("yangshijie",20,new MyDate(2001,7,23));
        Employee employee2 = new Employee("yanzhihang",18,new MyDate(2003,1,9));
        Employee employee3 = new Employee("chenkangjia",18,new MyDate(2003,1,9));
        Employee employee4 = new Employee("luoxingzhe",20,new MyDate(2002,7,23));
        Employee employee5 = new Employee("wangwei",20,new MyDate(2001,8,23));
        tree.add(employee1);
        tree.add(employee2);
        tree.add(employee3);
        tree.add(employee4);
        tree.add(employee5);
        Iterator iterator = tree.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }



    }

}
public class Employee  {
    private String name;
    private int age;
    private MyDate birthday;

    public Employee(String name, int age, MyDate birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public Employee() {

    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", 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;
    }

    
}

 

 

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Duck&踏风彡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值