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


相关文章
|
7月前
|
搜索推荐 Java API
一道Java集合排序题,HashMap排序,面试必备
一道Java集合排序题,HashMap排序,面试必备
|
3月前
|
安全 Java
Java TreeSet:基于红黑树的排序集合解析
Java TreeSet:基于红黑树的排序集合解析
|
8月前
TreeMap的排序
TreeMap的排序
40 0
|
6月前
|
C#
c#集合去重&排序常用方法
list和数组转Hashset跟SortedSet进行排序和去重,以及当Hashset和SortedSet存放的是类时如何进行自定义的排序和去重
47 2
|
9月前
TreeSet集合
TreeSet集合
29 0
|
11月前
TreeMap实现排序
TreeMap实现排序
集合排序-List集合
在Collections下有一个方法,sort()方法,不仅是数值,其他类型也有默认的排序方法
|
存储 Java 索引
Java中TreeSet集合、自然排序、比较器排序、成绩排序及不重复随机数案例
TreeSet集合、自然排序、比较器排序、成绩排序及不重复随机数案例的简单示例
151 0
Java中TreeSet集合、自然排序、比较器排序、成绩排序及不重复随机数案例
|
存储 算法 Java
TreeSet类的排序问题
TreeSet支持两种排序方法:自然排序和定制排序。TreeSet默认采用自然排序。1、自然排序    TreeSet会调用集合元素的compareTo(Object obj)方法来比较元素之间大小关系,然后将集合元素按升序排列,这种方式就是自然排序。
1630 0
|
消息中间件 前端开发 JavaScript
LinkedHashSet 有序且不能重复的集合
哈喽,大家好,我是指北君。 同 HashSet 与 HashMap 的关系一样,本篇文章所介绍的 LinkedHashSet 和 LinkedHashMap 也是一致的。在 JDK 集合框架中,类似 Set 集合通常都是由对应的 Map 类集合来实现的(TreeSet 和 TreeMap 同理),这里很重要的一个理论就是:Set 类集合是不允许重复的,而 Map 类集合的 key 也是不允许重复的,所以通常很容易就用 Map 类集合实现了 Set 类集合。
LinkedHashSet 有序且不能重复的集合