TreeSet的两种排序:自然排序和定制排序

向TreeSet集合中添加类对象时,类对象的类必须实现Comparable接口,重写compareTo()方法。

两个测试实体类

/**
 * 生日实体类
 */
public class MyDate {

    private int year;
    private int month;
    private int day;

    public MyDate() {
    }


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

    public int getYear() {
        return year;
    }

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

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

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

/**
 * Employee实体类
 */
public class Employee implements Comparable<Employee> {

    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(Employee o) {
        //根据name字段进行排序,从小到大进行排序
        return this.name.compareTo(o.name);
        //从大到小进行排序
//        return o.name.compareTo(this.name);
    }
}

1: 自然排序

//使用自然排序对Employee进行排序
//使Employee实现Comparable接口,并按照name字段进行排序
@Test
public void test1() {
  //创建一个Set集合
  TreeSet<Employee> set = new TreeSet<>();
  //创建Employee对象
  Employee e1 = new Employee("sanSan", 25, new MyDate(1996, 4, 5));
  Employee e2 = new Employee("liSi", 23, new MyDate(1998, 8, 25));
  Employee e3 = new Employee("wangWu", 26, new MyDate(1995, 9, 15));
  Employee e4 = new Employee("zhaoLiu", 24, new MyDate(1997, 8, 9));
  Employee e5 = new Employee("tianQi", 22, new MyDate(1999, 10, 5));
  //将Employee对象添加到Set集合中
  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());
  }
}

2: 定制排序

//使用定制排序,根据生日字段进行排序,生日从小到大进行排序
@Test
public void test2() {
  //创建一个Set集合
  TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {
    @Override
    public int compare(Employee o1, Employee o2) {
      //获取到生日
      MyDate birthday1 = o1.getBirthday();
      MyDate birthday2 = o2.getBirthday();

      //比较年,获取到年差
      int year = birthday1.getYear() - birthday2.getYear();
      if (year != 0) {
        return year;
      }
      //比较月,获取月差
      int month = birthday1.getMonth() - birthday2.getMonth();
      if (month != 0) {
        return month;
      }
      //比较日,获取日差
      return birthday1.getDay() - birthday2.getDay();
    }
  });
  //创建Employee对象
  Employee e1 = new Employee("sanSan", 24, new MyDate(1997, 4, 5));
  Employee e2 = new Employee("liSi", 23, new MyDate(1998, 8, 25));
  Employee e3 = new Employee("wangWu", 26, new MyDate(1995, 9, 15));
  Employee e4 = new Employee("zhaoLiu", 24, new MyDate(1997, 8, 9));
  Employee e5 = new Employee("tianQi", 22, new MyDate(1999, 10, 5));

  //将Employee对象添加到Set集合中
  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());
  }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值