TreeSet排序方式?

1,基于java bean来实现comparable接口,这种排序方式称为自然排序

public class Employee implements Comparable<Employee>{
    private int id;
    private String name;
    private int salary;

    public Employee(int id, String name, int salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public int compareTo(Employee o) {
        return this.getSalary().compareTo(o.getSalary());
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }


}

2,实例化TreeSet的时候实现comparator接口,这种排序方式是自定义排序

public class Test {

    public static void main(String[] args) {
        TreeSet<Employee> employees = new TreeSet<Employee>(new Comparator<Employee>() {
            public int compare(Employee o1, Employee o2) {
                return (int) (o1.getSalary() - o2.getSalary());
            }
        });
        employees.add(new Employee(3, "张三", -13001f));
        employees.add(new Employee(32, "张三", -13001.1f));
        employees.add(new Employee(31, "张三", 12999.6f));

        System.out.println(employees);
        {

        }
    }
}

目的都是对set进行组织的时候,自动完成数据排序工作。