I am able to insert duplicate entries in TreeSet. How to overcome this

后端 未结 3 1573
猫巷女王i
猫巷女王i 2021-01-14 16:15

I have a class called Employee which has employeeName and employeeId as its member variables.I am creating new Employee objects and then adding it

3条回答
  •  有刺的猬
    2021-01-14 16:43

    you should compare string not with ==, but with equals() method, and also you should override your compareTo method to compare with employeeName not with employeeId, if you want it in that way.

    (Employee)emp).employeeName.equals(this.employeeName)
    

    and

    public int compareTo(Employee emp) {
    
      return (this.employeeName-emp.employeeName);
    }
    

提交回复
热议问题