我正在尝试在 ArrayList 中使用 contains 方法,我在其中存储了 Employee 类型的对象。但是当我使用这个方法时,即使我在 Employee 类中实现了 equals 方法,它总是返回 false
这是我尝试过的:
员工等级:
public class Employee {
.
.
.
@Override
public boolean equals(Object o){
if(o instanceof Employee)
{
Employee e = (Employee) o;
return this.id==(e.id);
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + (int) (this.id ^ (this.id >>> 32));
return hash;
}
测试类:
public class Test {
LinkedList <Employee>list=new LinkedList <Employee>();
Employee e=new Employee();
long id=0;
e.setId(20710456);
list.add(e);
e.setId(30560432);
list.add(e);
public void Edit()
{
id=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the id of the
employee"));
e.setId(id);
System.out.print(list.contains(e.getId()));
if(list.contains(e.getId())){
.
.
//rest of the code
}
}
}