使用用户定义的对象排序hashset

时间:2017-09-27 10:44:15

标签: java sorting hashset defined

public class Employee {

    private int id;
    private String name;
    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;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + "]";
    }
    public Employee(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
}


Set<Employee> set = new HashSet<Employee>();
Employee e2 = new Employee(3,"Mohan");
Employee e3 = new Employee(4,"Shayam");
Employee e4 = new Employee(5,"rahul");
Employee e = new Employee(1,"rahul");
Employee e1 = new Employee(2,"ram");

当hashset包含一些用户定义的对象时,如何对此HashSet进行排序?

0 个答案:

没有答案