TreeSet的两种排序方式

简介: 示例:示例公用类:public class MyDate { private int month; private int year; private int day; @Override public String toString() { return "MyDate{" + "month=" + month + ", year=" + year +

 示例:

示例公用类:

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;
    }
}

image.gif

自然排序:在需要排序的属性的类中重写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;
    }
}

image.gif

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());
        }
    }
}

image.gif

示例二:定制排序,创建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());
        }
    }
}

image.gif

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;
    }
}

image.gif


相关文章
|
搜索推荐 Java API
一道Java集合排序题,HashMap排序,面试必备
一道Java集合排序题,HashMap排序,面试必备
|
17天前
|
存储 Java
深入探讨了Java集合框架中的HashSet和TreeSet,解析了两者在元素存储上的无序与有序特性。
【10月更文挑战第16天】本文深入探讨了Java集合框架中的HashSet和TreeSet,解析了两者在元素存储上的无序与有序特性。HashSet基于哈希表实现,添加元素时根据哈希值分布,遍历时顺序不可预测;而TreeSet利用红黑树结构,按自然顺序或自定义顺序存储元素,确保遍历时有序输出。文章还提供了示例代码,帮助读者更好地理解这两种集合类型的使用场景和内部机制。
33 3
|
20天前
|
存储 Java
Java集合框架中的HashSet和TreeSet,解释了它们如何分别实现无序和有序存储。
【10月更文挑战第13天】本文深入探讨了Java集合框架中的HashSet和TreeSet,解释了它们如何分别实现无序和有序存储。通过解析内部机制和示例代码,帮助读者理解这两种集合的特点和应用场景,从而更好地选择合适的集合类型满足实际需求。
27 3
TreeMap的排序
TreeMap的排序
74 0
|
C#
c#集合去重&排序常用方法
list和数组转Hashset跟SortedSet进行排序和去重,以及当Hashset和SortedSet存放的是类时如何进行自定义的排序和去重
91 2
自定义排序的常用方式
自定义排序的常用方式
|
存储 Java 索引
Java中TreeSet集合、自然排序、比较器排序、成绩排序及不重复随机数案例
TreeSet集合、自然排序、比较器排序、成绩排序及不重复随机数案例的简单示例
190 0
Java中TreeSet集合、自然排序、比较器排序、成绩排序及不重复随机数案例
|
存储 算法 Java
TreeSet类的排序问题
TreeSet支持两种排序方法:自然排序和定制排序。TreeSet默认采用自然排序。1、自然排序    TreeSet会调用集合元素的compareTo(Object obj)方法来比较元素之间大小关系,然后将集合元素按升序排列,这种方式就是自然排序。
1685 0