Java中集合Set接口TreeSet的应用(自然排序、定制排序)

    最近在学习Java的集合,一共分为两个接口——Collection和Map接口,Collection又分为List和Set两个子接口,实现Set接口的一共有三个类:HashSet、LinkedHashSet、TreeSet,这个主要写一点TreeSet的使用方法,在其中存的数据一定要属于同一个类,它主要就是涉及到两种排序方式——自然排序和定制排序。

通过一个例子来说明:

   MyDate 类包含: private 成员变量 year,month,day

/**
 * MyDate 类包含:
 * private 成员变量 year,month,day;并为每一个属性定义 getter, setter
 * @author asus
 * @create 2022--03--21 20:21
 */
public class MyDate {
    private int year;
    private int month;
    private int day;

    public int getMonth() {
        return month;
    }

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

    public int getDay() {
        return day;
    }

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

    public int getYear() {
        return year;
    }

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

    public MyDate() {
    }

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

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

   Employee 类包含:private 成员变量 name,age,birthday,其中 birthday 为MyDate类的对象

   创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中,分别按以下两种方式对集合中的元素进行排序,并遍历输出:

 1).第一种方式是实现Comparable接口,然后在Employee中重写CompareTo方法

/**
 *   MyDate 类的对象;
 * 定义一个 Employee 类。
 *  该类包含:private 成员变量 name,age,birthday,其中 birthday 为MyDate类的对象
 *   并为每一个属性定义 getter, setter 方法;
 *   并重写 toString 方法输出 name, age, birthday
 * @author asus
 * @create 2022--03--21 20:22
 */
public class Employee implements Comparable {
    private String name;
    private int age;
    private MyDate birthday;

    public Employee() {
    }

    public Employee(String name, int age, MyDate birthday) {
        this.name = name;
        this.age = age;
        this.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 String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if(o instanceof Employee){
            Employee e = (Employee) o;
            return (this.getName()).compareTo(((Employee) o).getName());
        }
        throw new RuntimeException("传入的数据类型不一致");
    }
}

在测试类中创建TreeSet的对象,添加数据,并且通过迭代器的方式遍历。(自然排序)

public class EmployeeTest {
    @Test
    public void test1(){
        TreeSet set = new TreeSet();


        Employee e1 = new Employee("Tom",56,new MyDate(1999,5,6));
        Employee e2 = new Employee("Mary",43,new MyDate(1998,10,6));
        Employee e3 = new Employee("David",56,new MyDate(1995,6,6));
        Employee e4 = new Employee("Asha",51,new MyDate(2000,5,15));
        Employee e5 = new Employee("Nancy",56,new MyDate(2001,8,6));

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

 2). 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序。(定制排序)

public class EmployeeTest {       
 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();

                    int minusYear  = b1.getYear()-b2.getYear();
                    if(minusYear != 0){
                        return  minusYear;
                    }
                    int minusMonth = b1.getMonth()-b2.getMonth();
                    if(minusMonth!=0){
                        return minusMonth;
                    }
                    return b1.getDay()-b2.getDay();


                }
                throw new RuntimeException("传入的数据类型不一致");
            }
        });

        Employee e1 = new Employee("liudehau",56,new MyDate(1965,5,6));
        Employee e2 = new Employee("zhangxueyou",43,new MyDate(1965,10,6));
        Employee e3 = new Employee("guofucheng",56,new MyDate(1965,6,6));
        Employee e4 = new Employee("liming",51,new MyDate(1965,5,15));
        Employee e5 = new Employee("liaochaowei",56,new MyDate(1965,8,6));

        set.add(e1);
        set.add(e2);
        set.add(e3);
        set.add(e4);
        set.add(e5);

        //用迭代器的方式来遍历
        Iterator<Employee> iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值